FileDeleteStrategypublic class FileDeleteStrategy extends Object Strategy for deleting files.
There is more than one way to delete a file.
You may want to limit access to certain directories, to only delete
directories if they are empty, or maybe to force deletion.
This class captures the strategy to use and is designed for user subclassing. |
Fields Summary |
---|
public static final FileDeleteStrategy | NORMALThe singleton instance for normal file deletion, which does not permit
the deletion of directories that are not empty. | public static final FileDeleteStrategy | FORCEThe singleton instance for forced file deletion, which always deletes,
even if the file represents a non-empty directory. | private final String | nameThe name of the strategy. |
Constructors Summary |
---|
protected FileDeleteStrategy(String name)Restricted constructor.
//-----------------------------------------------------------------------
this.name = name;
|
Methods Summary |
---|
public void | delete(java.io.File fileToDelete)Deletes the file object, which may be a file or a directory.
If the file does not exist, the method just returns.
Subclass writers should override {@link #doDelete(File)}, not this method.
if (fileToDelete.exists() && doDelete(fileToDelete) == false) {
throw new IOException("Deletion failed: " + fileToDelete);
}
| public boolean | deleteQuietly(java.io.File fileToDelete)Deletes the file object, which may be a file or a directory.
All IOException s are caught and false returned instead.
If the file does not exist or is null, true is returned.
Subclass writers should override {@link #doDelete(File)}, not this method.
if (fileToDelete == null || fileToDelete.exists() == false) {
return true;
}
try {
return doDelete(fileToDelete);
} catch (IOException ex) {
return false;
}
| protected boolean | doDelete(java.io.File fileToDelete)Actually deletes the file object, which may be a file or a directory.
This method is designed for subclasses to override.
The implementation may return either false or an IOException
when deletion fails. The {@link #delete(File)} and {@link #deleteQuietly(File)}
methods will handle either response appropriately.
A check has been made to ensure that the file will exist.
This implementation uses {@link File#delete()}.
return fileToDelete.delete();
| public java.lang.String | toString()Gets a string describing the delete strategy.
return "FileDeleteStrategy[" + name + "]";
|
|