org.springframework.core.io

## Class FileUrlResource

- java.lang.Object
- - org.springframework.core.io.AbstractResource
  - - org.springframework.core.io.AbstractFileResolvingResource
    - - org.springframework.core.io.UrlResource
      - - org.springframework.core.io.FileUrlResource

- All Implemented Interfaces:
    InputStreamSource, Resource, WritableResource

      public class FileUrlResource
      extends UrlResource
      implements WritableResource

  Subclass of `UrlResource` which assumes file resolution, to the degree of implementing the `WritableResource` interface for it. This resource variant also caches resolved `File` handles from `getFile()`.

  This is the class resolved by `DefaultResourceLoader` for a "file:..." URL location, allowing a downcast to `WritableResource` for it.

  Alternatively, for direct construction from a `File` handle or NIO `Path`, consider using `FileSystemResource`.

  Since:
    5.0.2
  Author:
    Juergen Hoeller

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `FileUrlResource(String location)` Create a new `FileUrlResource` based on the given file location, using the URL protocol "file".
    `FileUrlResource(URL url)` Create a new `FileUrlResource` based on the given URL object.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `Resource` | `createRelative(String relativePath)` This implementation creates a `UrlResource`, delegating to `UrlResource.createRelativeURL(String)` for adapting the relative path.
    `File` | `getFile()` This implementation returns a File reference for the underlying URL/URI, provided that it refers to a file in the file system.
    `OutputStream` | `getOutputStream()` Return an `OutputStream` for the underlying resource, allowing to (over-)write its content.
    `boolean` | `isWritable()` Indicate whether the contents of this resource can be written via `WritableResource.getOutputStream()`.
    `WritableByteChannel` | `writableChannel()` Return a `WritableByteChannel`.

    - ### Methods inherited from class org.springframework.core.io.UrlResource

      `createRelativeURL, equals, getDescription, getFilename, getInputStream, getURI, getURL, hashCode, isFile`

    - ### Methods inherited from class org.springframework.core.io.AbstractFileResolvingResource

      `contentLength, customizeConnection, customizeConnection, exists, getFile, getFileForLastModifiedCheck, isFile, isReadable, lastModified, readableChannel`

    - ### Methods inherited from class org.springframework.core.io.AbstractResource

      `isOpen, toString`

    - ### Methods inherited from class java.lang.Object

      `clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

    - ### Methods inherited from interface org.springframework.core.io.Resource

      `contentLength, exists, getDescription, getFilename, getURI, getURL, isFile, isOpen, isReadable, lastModified, readableChannel`

    - ### Methods inherited from interface org.springframework.core.io.InputStreamSource

      `getInputStream`

- - ### Constructor Detail

    - #### FileUrlResource

          public FileUrlResource(URL url)

      Create a new `FileUrlResource` based on the given URL object.

      Note that this does not enforce "file" as URL protocol. If a protocol is known to be resolvable to a file, it is acceptable for this purpose.

      Parameters:
        `url` - a URL
      See Also:
        `ResourceUtils.isFileURL(URL)`, `getFile()`

    - #### FileUrlResource

          public FileUrlResource(String location)
                          throws MalformedURLException

      Create a new `FileUrlResource` based on the given file location, using the URL protocol "file".

      The given parts will automatically get encoded if necessary.

      Parameters:
        `location` - the location (i.e. the file path within that protocol)
      Throws:
        `MalformedURLException` - if the given URL specification is not valid
      See Also:
        `UrlResource(String, String)`, `ResourceUtils.URL_PROTOCOL_FILE`

  - ### Method Detail

    - #### getFile

          public File getFile()
                       throws IOException

      Description copied from class: `UrlResource`
      This implementation returns a File reference for the underlying URL/URI, provided that it refers to a file in the file system.
      Specified by:
        `getFile` in interface `Resource`
      Overrides:
        `getFile` in class `UrlResource`
      Throws:
        `FileNotFoundException` - if the resource cannot be resolved as absolute file path, i.e. if the resource is not available in a file system
        `IOException` - in case of general resolution/reading failures
      See Also:
        `ResourceUtils.getFile(java.net.URL, String)`

    - #### isWritable

          public boolean isWritable()

      Description copied from interface: `WritableResource`
      Indicate whether the contents of this resource can be written via `WritableResource.getOutputStream()`.

      Will be `true` for typical resource descriptors; note that actual content writing may still fail when attempted. However, a value of `false` is a definitive indication that the resource content cannot be modified.

      Specified by:
        `isWritable` in interface `WritableResource`
      See Also:
        `WritableResource.getOutputStream()`, `Resource.isReadable()`

    - #### getOutputStream

          public OutputStream getOutputStream()
                                       throws IOException

      Description copied from interface: `WritableResource`
      Return an `OutputStream` for the underlying resource, allowing to (over-)write its content.
      Specified by:
        `getOutputStream` in interface `WritableResource`
      Throws:
        `IOException` - if the stream could not be opened
      See Also:
        `InputStreamSource.getInputStream()`

    - #### writableChannel

          public WritableByteChannel writableChannel()
                                              throws IOException

      Description copied from interface: `WritableResource`
      Return a `WritableByteChannel`.

      It is expected that each call creates a fresh channel.

      The default implementation returns `Channels.newChannel(OutputStream)` with the result of `WritableResource.getOutputStream()`.

      Specified by:
        `writableChannel` in interface `WritableResource`
      Returns:
        the byte channel for the underlying resource (must not be `null`)
      Throws:
        `FileNotFoundException` - if the underlying resource doesn't exist
        `IOException` - if the content channel could not be opened
      See Also:
        `WritableResource.getOutputStream()`

    - #### createRelative

          public Resource createRelative(String relativePath)
                                  throws MalformedURLException

      Description copied from class: `UrlResource`
      This implementation creates a `UrlResource`, delegating to `UrlResource.createRelativeURL(String)` for adapting the relative path.
      Specified by:
        `createRelative` in interface `Resource`
      Overrides:
        `createRelative` in class `UrlResource`
      Parameters:
        `relativePath` - the relative path (relative to this resource)
      Returns:
        the resource handle for the relative resource
      Throws:
        `MalformedURLException`
      See Also:
        `UrlResource.createRelativeURL(String)`