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

public Class PathResource

extends AbstractResource
implements WritableResource
Class Inheritance
All Implemented Interfaces
org.springframework.core.io.WritableResource, org.springframework.core.io.Resource, org.springframework.core.io.InputStreamSource
Imports
java.io.File, .FileNotFoundException, .IOException, .InputStream, .OutputStream, java.net.URI, .URL, java.nio.channels.ReadableByteChannel, .WritableByteChannel, java.nio.charset.Charset, java.nio.file.Files, .NoSuchFileException, .OpenOption, .Path, .Paths, .StandardOpenOption, org.springframework.lang.Nullable, org.springframework.util.Assert

Resource implementation for java.nio.file.Path handles, performing all operations and transformations via the Path API. Supports resolution as a File and also as a URL. Implements the extended WritableResource interface.

Note

As of 5.1, java.nio.file.Path support is also available in FileSystemResource, applying Spring's standard String-based path transformations but performing all operations via the java.nio.file.Files API. This PathResource is effectively a pure java.nio.path.Path based alternative with different createRelative behavior.

Authors
Philippe Marschall, Juergen Hoeller
Since
4.0
See Also
java.nio.file.Path, java.nio.file.Files, FileSystemResource

Field Summary

Modifier and TypeField and Description
private final Path

Constructor Summary

AccessConstructor and Description
public
PathResource(Path
a Path handle
path
)

Create a new PathResource from a Path handle.

public
PathResource(String
a path
path
)

Create a new PathResource from a path string.

public
PathResource(URI
a path URI
uri
)

Create a new PathResource from a URI.

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's 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 PathResource, 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 Path references.
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.

Return a description for this resource, to be used for error output when working with the resource.
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 InputStream for the underlying file.
public OutputStream
getOutputStream()

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

This implementation opens an OutputStream 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 Path reference.
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's timestamp.
public ReadableByteChannel
public WritableByteChannel
writableChannel()

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

This implementation opens a WritableByteChannel for the underlying file.
Inherited from org.springframework.core.io.AbstractResource:
getFileForLastModifiedCheckisOpentoString

Field Detail

pathback to summary
private final Path path

Constructor Detail

PathResourceback to summary
public PathResource(Path path)

Create a new PathResource from a Path handle.

Note

Unlike FileSystemResource, when building relative resources via createRelative, the relative path will be built underneath the given root: e.g. Paths.get("C:/dir1/"), relative path "dir2" → "C:/dir1/dir2"!

Parameters
path:Path

a Path handle

PathResourceback to summary
public PathResource(String path)

Create a new PathResource from a path string.

Note

Unlike FileSystemResource, when building relative resources via createRelative, the relative path will be built underneath the given root: e.g. Paths.get("C:/dir1/"), relative path "dir2" → "C:/dir1/dir2"!

Parameters
path:String

a path

See Also
java.nio.file.Paths#get(String, String...)
PathResourceback to summary
public PathResource(URI uri)

Create a new PathResource from a URI.

Note

Unlike FileSystemResource, when building relative resources via createRelative, the relative path will be built underneath the given root: e.g. Paths.get("C:/dir1/"), relative path "dir2" → "C:/dir1/dir2"!

Parameters
uri:URI

a path URI

See Also
java.nio.file.Paths#get(URI)

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's 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 PathResource, 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
java.nio.file.Path#resolve(String)
equalsback to summary
public boolean equals(Object other)

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

This implementation compares the underlying Path references.

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
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.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.

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

Return a description for this resource, to be used for error output when working with the resource.

Implementations are also encouraged to return this value from their toString method.

Annotations
@Override
getFileback to summary
public File getFile() throws IOException

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

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

This implementation returns the underlying File reference.

Annotations
@Override
Exceptions
IOException:

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

in case of general resolution/reading failures

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.nio.file.Path#getFileName()
getInputStreamback to summary
public InputStream getInputStream() throws IOException

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

This implementation opens an InputStream 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.spi.FileSystemProvider#newInputStream(Path, OpenOption...)
getOutputStreamback to summary
public OutputStream getOutputStream() throws IOException

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

This implementation opens an OutputStream 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.spi.FileSystemProvider#newOutputStream(Path, 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.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.nio.file.Path#toUri(), java.net.URI#toURL()
hashCodeback to summary
public int hashCode()

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

This implementation returns the hash code of the underlying Path reference.

Returns:int

Doc from java.lang.Object.hashCode.

a hash code value for this object.

Annotations
@Override
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.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.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's timestamp.

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)

See Also
java.nio.file.Files#getLastModifiedTime(Path, java.nio.file.LinkOption...)
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 ReadableByteChannel 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
Files#newByteChannel(Path, OpenOption...)
writableChannelback to summary
public WritableByteChannel writableChannel() throws IOException

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

This implementation opens a WritableByteChannel 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
Files#newByteChannel(Path, OpenOption...)