Top Description Inners Fields Constructors Methods
org.apache.commons.io

public Class FileDeleteStrategy

extends Object
Class Inheritance
Known Direct Subclasses
org.apache.commons.io.FileDeleteStrategy.ForceFileDeleteStrategy
Imports
java.io.File, .IOException

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
Since
Commons IO 1.3
Version
$Id: FileDeleteStrategy.java 453903 2006-10-07 13:47:06Z scolebourne $

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
FileDeleteStrategy.ForceFileDeleteStrategy

Force file deletion strategy.

Field Summary

Modifier and TypeField and Description
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.

public static final FileDeleteStrategy
NORMAL

The singleton instance for normal file deletion, which does not permit the deletion of directories that are not empty.

Constructor Summary

AccessConstructor and Description
protected
FileDeleteStrategy(String
the name by which the strategy is known
name
)

Restricted constructor.

Method Summary

Modifier and TypeMethod and Description
public void
delete(File
the file to delete, not null
fileToDelete
)

Deletes the file object, which may be a file or a directory.

public boolean

Returns:

true if the file was deleted, or there was no such file
deleteQuietly
(File
the file to delete, null returns true
fileToDelete
)

Deletes the file object, which may be a file or a directory.

protected boolean

Returns:

true if the file was deleteds
doDelete
(File
the file to delete, exists, not null
fileToDelete
)

Actually deletes the file object, which may be a file or a directory.

public String

Returns:

a string describing the delete strategy
toString
()

Overrides java.lang.Object.toString.

Gets a string describing the delete strategy.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

FORCEback to summary
public static final FileDeleteStrategy FORCE

The singleton instance for forced file deletion, which always deletes, even if the file represents a non-empty directory.

nameback to summary
private final String name

The name of the strategy.

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

Constructor Detail

FileDeleteStrategyback to summary
protected FileDeleteStrategy(String name)

Restricted constructor.

Parameters
name:String

the name by which the strategy is known

Method Detail

deleteback to summary
public void delete(File fileToDelete) throws IOException

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 doDelete(File), not this method.

Parameters
fileToDelete:File

the file to delete, not null

Exceptions
IOException:
if an error occurs during file deletion
NullPointerException:
if the file is null
deleteQuietlyback to summary
public boolean deleteQuietly(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 doDelete(File), not this method.

Parameters
fileToDelete:File

the file to delete, null returns true

Returns:boolean

true if the file was deleted, or there was no such file

doDeleteback to summary
protected boolean doDelete(File fileToDelete) throws IOException

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 delete(File) and deleteQuietly(File) methods will handle either response appropriately. A check has been made to ensure that the file will exist.

This implementation uses File#delete().

Parameters
fileToDelete:File

the file to delete, exists, not null

Returns:boolean

true if the file was deleteds

Exceptions
IOException:
if an error occurs during file deletion
NullPointerException:
if the file is null
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Gets a string describing the delete strategy.

Returns:String

a string describing the delete strategy

org.apache.commons.io back to summary

pack-priv Class FileDeleteStrategy.ForceFileDeleteStrategy

extends FileDeleteStrategy
Class Inheritance

Force file deletion strategy.

Field Summary

Inherited from org.apache.commons.io.FileDeleteStrategy:
FORCENORMAL

Constructor Summary

AccessConstructor and Description
pack-priv
ForceFileDeleteStrategy()

Default Constructor

Method Summary

Modifier and TypeMethod and Description
protected boolean

Returns:

Always returns true
doDelete
(File
the file to delete, not null
fileToDelete
)

Overrides org.apache.commons.io.FileDeleteStrategy.doDelete.

Deletes the file object.
Inherited from org.apache.commons.io.FileDeleteStrategy:
deletedeleteQuietlytoString

Constructor Detail

ForceFileDeleteStrategyback to summary
pack-priv ForceFileDeleteStrategy()

Default Constructor

Method Detail

doDeleteback to summary
protected boolean doDelete(File fileToDelete) throws IOException

Overrides org.apache.commons.io.FileDeleteStrategy.doDelete.

Deletes the file object.

This implementation uses FileUtils.forceDelete() if the file exists.

Parameters
fileToDelete:File

the file to delete, not null

Returns:boolean

Always returns true

Exceptions
IOException:
if an error occurs during file deletion
NullPointerException:
if the file is null