(阅读笔记)FileSystemResource
(2010-11-21 16:50:00)
package org.springframework.core.io;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
public class FileSystemResource extends AbstractResource {
private
final File file;
private
final String path;
//Create a
new FileSystemResource from a File handle.
public
FileSystemResource(File file) {
Assert.notNull(file, "File must not be
null");
this.file = file;
this.path =
StringUtils.cleanPath(file.getPath());
}
// Create a
new FileSystemResource from a file path.
public
FileSystemResource(String path) {
Assert.notNull(path, "Path must not be
null");
this.file = new File(path);
this.path = StringUtils.cleanPath(path);
}
// Return
the file path for this resource.
public final
String getPath() {
return this.path;
}
// This
implementation returns whether the underlying file exists.
public
boolean exists() {
return this.file.exists();
}
// 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 isReadable() {
return (this.file.canRead()
&& !this.file.isDirectory());
}
//This
implementation opens a FileInputStream for the underlying
file.
public
InputStream getInputStream() throws IOException {
return new FileInputStream(this.file);
}
// This
implementation returns a URL for the underlying file.
public URL
getURL() throws IOException {
return this.file.toURI().toURL();
}
//This
implementation returns a URI for the underlying file.
public URI
getURI() throws IOException {
return this.file.toURI();
}
// This
implementation returns the underlying File reference.
public File
getFile() {
return this.file;
}
// This
implementation creates a FileSystemResource, applying the given
path
* relative
to the path of the underlying file of this resource
descriptor.
public
Resource createRelative(String relativePath) {
String pathToUse =
StringUtils.applyRelativePath(this.path, relativePath);
return new FileSystemResource(pathToUse);
}
// This
implementation returns the name of the file.
public
String getFilename() {
return this.file.getName();
}
/ This
implementation returns a description that includes the
absolute
* path of
the file.
public
String getDescription() {
return "file [" + this.file.getAbsolutePath() +
"]";
}
public
boolean equals(Object obj) {
return (obj == this ||
(obj instanceof FileSystemResource
&&
this.path.equals(((FileSystemResource) obj).path)));