Top Description Fields Constructors Methods
org.springframework.core.io

public Class FileSystemResource

extends AbstractResource
implements WritableResource
Class Inheritance
All Implemented Interfaces
org.springframework.core.io.WritableResource, org.springframework.core.io.Resource, org.springframework.core.io.InputStreamSource
Known Direct Subclasses
org.springframework.core.io.FileSystemResourceLoader.FileSystemContextResource
Imports
java.io.File, .FileNotFoundException, .IOException, .InputStream, .OutputStream, java.net.URI, .URISyntaxException, .URL, java.nio.channels.FileChannel, .ReadableByteChannel, .WritableByteChannel, java.nio.charset.Charset, java.nio.file.FileSystem, .Files, .NoSuchFileException, .Path, .StandardOpenOption, org.springframework.lang.Nullable, org.springframework.util.Assert, .ResourceUtils, .StringUtils

Resource implementation for java.io.File and java.nio.file.Path handles with a file system target. Supports resolution as a File and also as a URL. Implements the extended WritableResource interface.

Note

As of Spring Framework 5.0, this Resource implementation uses NIO.2 API for read/write interactions. As of 5.1, it may be constructed with a java.nio.file.Path handle in which case it will perform all file system interactions via NIO.2, only resorting to File on getFile().

Authors
Juergen Hoeller, Sam Brannen
Since
28.12.2003
See Also
FileSystemResource(String), FileSystemResource(File), FileSystemResource(Path), java.io.File, java.nio.file.Files

Field Summary

Modifier and TypeField and Description
private final File
private final Path
private final String

Constructor Summary

AccessConstructor and Description
public
FileSystemResource(String
a file path
path
)

Create a new FileSystemResource from a file path.

public
FileSystemResource(File
a File handle
file
)

Create a new FileSystemResource from a File handle.

public
FileSystemResource(Path
a Path handle to a file
filePath
)

Create a new FileSystemResource from a Path handle, performing all file system interactions via NIO.2 instead of File.

public
FileSystemResource(FileSystem
the FileSystem to locate the path within
fileSystem
,
String
a file path
path
)

Create a new FileSystemResource from a FileSystem handle, locating the specified path.

Method Summary

Modifier and TypeMethod and Description
public long
contentLength()

Overrides org.springframework.core.io.AbstractResource.contentLength.

Implements org.springframework.core.io.Resource.contentLength.

This implementation returns the underlying File/Path length.

public Resource
createRelative(String
the relative path (relative to this resource)
relativePath
)

Overrides org.springframework.core.io.AbstractResource.createRelative.

Implements org.springframework.core.io.Resource.createRelative.

This implementation creates a FileSystemResource, applying the given path relative to the path of the underlying file of this resource descriptor.

public boolean
equals(Object
the reference object with which to compare.
other
)

Overrides org.springframework.core.io.AbstractResource.equals.

This implementation compares the underlying file paths.

public boolean
exists()

Overrides org.springframework.core.io.AbstractResource.exists.

Implements org.springframework.core.io.Resource.exists.

This implementation returns whether the underlying file exists.

public byte[]
getContentAsByteArray()

Overrides default org.springframework.core.io.Resource.getContentAsByteArray.

Return the contents of this resource as a byte array.

public String
getContentAsString(Charset
the charset to use for decoding
charset
)

Overrides default org.springframework.core.io.Resource.getContentAsString.

Return the contents of this resource as a string, using the specified charset.

public String
getDescription()

Implements org.springframework.core.io.Resource.getDescription.

This implementation returns a description that includes the absolute path of the file.

public File
getFile()

Overrides org.springframework.core.io.AbstractResource.getFile.

Implements org.springframework.core.io.Resource.getFile.

This implementation returns the underlying File reference.

public String
getFilename()

Overrides org.springframework.core.io.AbstractResource.getFilename.

Implements org.springframework.core.io.Resource.getFilename.

This implementation returns the name of the file.

public InputStream
getInputStream()

Implements org.springframework.core.io.InputStreamSource.getInputStream.

This implementation opens an NIO file stream for the underlying file.

public OutputStream
getOutputStream()

Implements org.springframework.core.io.WritableResource.getOutputStream.

This implementation opens a FileOutputStream for the underlying file.

public final String
getPath()

Return the file path for this resource.

public URI
getURI()

Overrides org.springframework.core.io.AbstractResource.getURI.

Implements org.springframework.core.io.Resource.getURI.

This implementation returns a URI for the underlying file.

public URL
getURL()

Overrides org.springframework.core.io.AbstractResource.getURL.

Implements org.springframework.core.io.Resource.getURL.

This implementation returns a URL for the underlying file.

public int
hashCode()

Overrides org.springframework.core.io.AbstractResource.hashCode.

This implementation returns the hash code of the underlying file path.

public boolean
isFile()

Overrides org.springframework.core.io.AbstractResource.isFile.

Overrides default org.springframework.core.io.Resource.isFile.

This implementation always indicates a file.

public boolean
isReadable()

Overrides org.springframework.core.io.AbstractResource.isReadable.

Overrides default org.springframework.core.io.Resource.isReadable.

This implementation checks whether the underlying file is marked as readable (and corresponds to an actual file with content, not to a directory).

public boolean
isWritable()

Overrides default org.springframework.core.io.WritableResource.isWritable.

This implementation checks whether the underlying file is marked as writable (and corresponds to an actual file with content, not to a directory).

public long
lastModified()

Overrides org.springframework.core.io.AbstractResource.lastModified.

Implements org.springframework.core.io.Resource.lastModified.

This implementation returns the underlying File/Path last-modified time.

public ReadableByteChannel
readableChannel()

Overrides org.springframework.core.io.AbstractResource.readableChannel.

Overrides default org.springframework.core.io.Resource.readableChannel.

This implementation opens a FileChannel for the underlying file.

public WritableByteChannel
writableChannel()

Overrides default org.springframework.core.io.WritableResource.writableChannel.

This implementation opens a FileChannel for the underlying file.

Inherited from org.springframework.core.io.AbstractResource:
getFileForLastModifiedCheckisOpentoString

Field Detail

fileback to summary
private final File file
Annotations
@Nullable
filePathback to summary
private final Path filePath
pathback to summary
private final String path

Constructor Detail

FileSystemResourceback to summary
public FileSystemResource(String path)

Create a new FileSystemResource from a file path.

Note

When building relative resources via createRelative, it makes a difference whether the specified resource base path here ends with a slash or not. In the case of "C:/dir1/", relative paths will be built underneath that root: e.g. relative path "dir2" → "C:/dir1/dir2". In the case of "C:/dir1", relative paths will apply at the same directory level: relative path "dir2" → "C:/dir2".

Parameters
path:String

a file path

See Also
FileSystemResource(Path)
FileSystemResourceback to summary
public FileSystemResource(File file)

Create a new FileSystemResource from a File handle.

Note

When building relative resources via createRelative, the relative path will apply at the same directory level: e.g. new File("C:/dir1"), relative path "dir2" → "C:/dir2"! If you prefer to have relative paths built underneath the given root directory, use the constructor with a file path to append a trailing slash to the root path: "C:/dir1/", which indicates this directory as root for all relative paths.

Parameters
file:File

a File handle

See Also
FileSystemResource(Path), getFile()
FileSystemResourceback to summary
public FileSystemResource(Path filePath)

Create a new FileSystemResource from a Path handle, performing all file system interactions via NIO.2 instead of File.

In contrast to PathResource, this variant strictly follows the general FileSystemResource conventions, in particular in terms of path cleaning and createRelative(String) handling.

Note

When building relative resources via createRelative, the relative path will apply at the same directory level: e.g. Paths.get("C:/dir1"), relative path "dir2" → "C:/dir2"! If you prefer to have relative paths built underneath the given root directory, use the constructor with a file path to append a trailing slash to the root path: "C:/dir1/", which indicates this directory as root for all relative paths. Alternatively, consider using PathResource#PathResource(Path) for java.nio.path.Path resolution in createRelative, always nesting relative paths.

Parameters
filePath:Path

a Path handle to a file

Since
5.1
See Also
FileSystemResource(File)
FileSystemResourceback to summary
public FileSystemResource(FileSystem fileSystem, String path)

Create a new FileSystemResource from a FileSystem handle, locating the specified path.

This is an alternative to FileSystemResource(String), performing all file system interactions via NIO.2 instead of File.

Parameters
fileSystem:FileSystem

the FileSystem to locate the path within

path:String

a file path

Since
5.1.1
See Also
FileSystemResource(File)

Method Detail

contentLengthback to summary
public long contentLength() throws IOException

Overrides org.springframework.core.io.AbstractResource.contentLength.

Implements org.springframework.core.io.Resource.contentLength.

This implementation returns the underlying File/Path length.

Annotations
@Override
Exceptions
IOException:

Doc from org.springframework.core.io.Resource.contentLength.

if the resource cannot be resolved (in the file system or as some other known physical resource type)

createRelativeback to summary
public Resource createRelative(String relativePath)

Overrides org.springframework.core.io.AbstractResource.createRelative.

Implements org.springframework.core.io.Resource.createRelative.

This implementation creates a FileSystemResource, applying the given path relative to the path of the underlying file of this resource descriptor.

Parameters
relativePath:String

Doc from org.springframework.core.io.Resource.createRelative.

the relative path (relative to this resource)

Returns:Resource

Doc from org.springframework.core.io.Resource.createRelative.

the resource handle for the relative resource

Annotations
@Override
See Also
org.springframework.util.StringUtils#applyRelativePath(String, String)
equalsback to summary
public boolean equals(Object other)

Overrides org.springframework.core.io.AbstractResource.equals.

This implementation compares the underlying file paths.

Parameters
other:Object

Doc from java.lang.Object.equals.

the reference object with which to compare.

Returns:boolean

Doc from java.lang.Object.equals.

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
See Also
getPath()
existsback to summary
public boolean exists()

Overrides org.springframework.core.io.AbstractResource.exists.

Implements org.springframework.core.io.Resource.exists.

This implementation returns whether the underlying file exists.

Annotations
@Override
See Also
java.io.File#exists(), java.nio.file.Files#exists(Path, java.nio.file.LinkOption...)
getContentAsByteArrayback to summary
public byte[] getContentAsByteArray() throws IOException

Overrides default org.springframework.core.io.Resource.getContentAsByteArray.

Doc from org.springframework.core.io.Resource.getContentAsByteArray.

Return the contents of this resource as a byte array.

Returns:byte[]

the contents of this resource as byte array

Annotations
@Override
Exceptions
IOException:
in case of general resolution/reading failures
getContentAsStringback to summary
public String getContentAsString(Charset charset) throws IOException

Overrides default org.springframework.core.io.Resource.getContentAsString.

Doc from org.springframework.core.io.Resource.getContentAsString.

Return the contents of this resource as a string, using the specified charset.

Parameters
charset:Charset

the charset to use for decoding

Returns:String

the contents of this resource as a String

Annotations
@Override
Exceptions
IOException:
in case of general resolution/reading failures
getDescriptionback to summary
public String getDescription()

Implements org.springframework.core.io.Resource.getDescription.

This implementation returns a description that includes the absolute path of the file.

Annotations
@Override
See Also
java.io.File#getAbsolutePath(), java.nio.file.Path#toAbsolutePath()
getFileback to summary
public File getFile()

Overrides org.springframework.core.io.AbstractResource.getFile.

Implements org.springframework.core.io.Resource.getFile.

This implementation returns the underlying File reference.

Annotations
@Override
getFilenameback to summary
public String getFilename()

Overrides org.springframework.core.io.AbstractResource.getFilename.

Implements org.springframework.core.io.Resource.getFilename.

This implementation returns the name of the file.

Annotations
@Override
See Also
java.io.File#getName(), java.nio.file.Path#getFileName()
getInputStreamback to summary
public InputStream getInputStream() throws IOException

Implements org.springframework.core.io.InputStreamSource.getInputStream.

This implementation opens an NIO file stream for the underlying file.

Returns:InputStream

Doc from org.springframework.core.io.InputStreamSource.getInputStream.

the input stream for the underlying resource (must not be null)

Annotations
@Override
Exceptions
IOException:

Doc from org.springframework.core.io.InputStreamSource.getInputStream.

if the content stream could not be opened

See Also
java.nio.file.Files#newInputStream(Path, java.nio.file.OpenOption...)
getOutputStreamback to summary
public OutputStream getOutputStream() throws IOException

Implements org.springframework.core.io.WritableResource.getOutputStream.

This implementation opens a FileOutputStream for the underlying file.

Annotations
@Override
Exceptions
IOException:

Doc from org.springframework.core.io.WritableResource.getOutputStream.

if the stream could not be opened

See Also
java.nio.file.Files#newOutputStream(Path, java.nio.file.OpenOption...)
getPathback to summary
public final String getPath()

Return the file path for this resource.

getURIback to summary
public URI getURI() throws IOException

Overrides org.springframework.core.io.AbstractResource.getURI.

Implements org.springframework.core.io.Resource.getURI.

This implementation returns a URI for the underlying file.

Annotations
@Override
Exceptions
IOException:

Doc from org.springframework.core.io.Resource.getURI.

if the resource cannot be resolved as URI, i.e. if the resource is not available as a descriptor

See Also
java.io.File#toURI(), java.nio.file.Path#toUri()
getURLback to summary
public URL getURL() throws IOException

Overrides org.springframework.core.io.AbstractResource.getURL.

Implements org.springframework.core.io.Resource.getURL.

This implementation returns a URL for the underlying file.

Annotations
@Override
Exceptions
IOException:

Doc from org.springframework.core.io.Resource.getURL.

if the resource cannot be resolved as URL, i.e. if the resource is not available as a descriptor

See Also
java.io.File#toURI(), java.nio.file.Path#toUri()
hashCodeback to summary
public int hashCode()

Overrides org.springframework.core.io.AbstractResource.hashCode.

This implementation returns the hash code of the underlying file path.

Returns:int

Doc from java.lang.Object.hashCode.

a hash code value for this object

Annotations
@Override
See Also
getPath()
isFileback to summary
public boolean isFile()

Overrides org.springframework.core.io.AbstractResource.isFile.

Overrides default org.springframework.core.io.Resource.isFile.

This implementation always indicates a file.

Annotations
@Override
isReadableback to summary
public boolean isReadable()

Overrides org.springframework.core.io.AbstractResource.isReadable.

Overrides default org.springframework.core.io.Resource.isReadable.

This implementation checks whether the underlying file is marked as readable (and corresponds to an actual file with content, not to a directory).

Annotations
@Override
See Also
java.io.File#canRead(), java.io.File#isDirectory(), java.nio.file.Files#isReadable(Path), java.nio.file.Files#isDirectory(Path, java.nio.file.LinkOption...)
isWritableback to summary
public boolean isWritable()

Overrides default org.springframework.core.io.WritableResource.isWritable.

This implementation checks whether the underlying file is marked as writable (and corresponds to an actual file with content, not to a directory).

Annotations
@Override
See Also
java.io.File#canWrite(), java.io.File#isDirectory(), java.nio.file.Files#isWritable(Path), java.nio.file.Files#isDirectory(Path, java.nio.file.LinkOption...)
lastModifiedback to summary
public long lastModified() throws IOException

Overrides org.springframework.core.io.AbstractResource.lastModified.

Implements org.springframework.core.io.Resource.lastModified.

This implementation returns the underlying File/Path last-modified time.

Annotations
@Override
Exceptions
IOException:

Doc from org.springframework.core.io.Resource.lastModified.

if the resource cannot be resolved (in the file system or as some other known physical resource type)

readableChannelback to summary
public ReadableByteChannel readableChannel() throws IOException

Overrides org.springframework.core.io.AbstractResource.readableChannel.

Overrides default org.springframework.core.io.Resource.readableChannel.

This implementation opens a FileChannel for the underlying file.

Returns:ReadableByteChannel

Doc from org.springframework.core.io.Resource.readableChannel.

the byte channel for the underlying resource (must not be null)

Annotations
@Override
Exceptions
IOException:

Doc from org.springframework.core.io.Resource.readableChannel.

if the content channel could not be opened

See Also
java.nio.channels.FileChannel
writableChannelback to summary
public WritableByteChannel writableChannel() throws IOException

Overrides default org.springframework.core.io.WritableResource.writableChannel.

This implementation opens a FileChannel for the underlying file.

Returns:WritableByteChannel

Doc from org.springframework.core.io.WritableResource.writableChannel.

the byte channel for the underlying resource (must not be null)

Annotations
@Override
Exceptions
IOException:

Doc from org.springframework.core.io.WritableResource.writableChannel.

if the content channel could not be opened

See Also
java.nio.channels.FileChannel