FileDocCategorySizeDatePackage
FileDeleteStrategy.javaAPI DocAndroid 1.5 API5914Wed May 06 22:42:46 BST 2009org.apache.commons.io

FileDeleteStrategy

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

author
Stephen Colebourne
version
$Id: FileDeleteStrategy.java 453903 2006-10-07 13:47:06Z scolebourne $
since
Commons IO 1.3

Fields Summary
public static final FileDeleteStrategy
NORMAL
The singleton instance for normal file deletion, which does not permit the deletion of directories that are not empty.
public static final FileDeleteStrategy
FORCE
The singleton instance for forced file deletion, which always deletes, even if the file represents a non-empty directory.
private final String
name
The name of the strategy.
Constructors Summary
protected FileDeleteStrategy(String name)
Restricted constructor.

param
name the name by which the strategy is known


    //-----------------------------------------------------------------------
                      
       
        this.name = name;
    
Methods Summary
public voiddelete(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.

param
fileToDelete the file to delete, not null
throws
NullPointerException if the file is null
throws
IOException if an error occurs during file deletion

        if (fileToDelete.exists() && doDelete(fileToDelete) == false) {
            throw new IOException("Deletion failed: " + fileToDelete);
        }
    
public booleandeleteQuietly(java.io.File fileToDelete)
Deletes the file object, which may be a file or a directory. All IOExceptions 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.

param
fileToDelete the file to delete, null returns true
return
true if the file was deleted, or there was no such file

        if (fileToDelete == null || fileToDelete.exists() == false) {
            return true;
        }
        try {
            return doDelete(fileToDelete);
        } catch (IOException ex) {
            return false;
        }
    
protected booleandoDelete(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()}.

param
fileToDelete the file to delete, exists, not null
return
true if the file was deleteds
throws
NullPointerException if the file is null
throws
IOException if an error occurs during file deletion

        return fileToDelete.delete();
    
public java.lang.StringtoString()
Gets a string describing the delete strategy.

return
a string describing the delete strategy

        return "FileDeleteStrategy[" + name + "]";