FileDeleteStrategy (Commons IO 2.5)
Date: 2016-12-01

org.apache.commons.io

## Class FileDeleteStrategy

- java.lang.Object
- - org.apache.commons.io.FileDeleteStrategy

- Direct Known Subclasses:
    FileDeleteStrategy.ForceFileDeleteStrategy

      public class FileDeleteStrategy
      extends java.lang.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.

  Since:
    1.3
  Version:
    $Id: FileDeleteStrategy.java 1563227 2014-01-31 19:45:30Z ggregory $

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `(package private) static class` | `FileDeleteStrategy.ForceFileDeleteStrategy` Force file deletion strategy.

  - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static FileDeleteStrategy` | `FORCE` The singleton instance for forced file deletion, which always deletes, even if the file represents a non-empty directory.
    `private java.lang.String` | `name` The name of the strategy.
    `static FileDeleteStrategy` | `NORMAL` The singleton instance for normal file deletion, which does not permit the deletion of directories that are not empty.

  - ### Constructor Summary

    Constructors
    Modifier | Constructor and Description
    --- | ---
    `protected` | `FileDeleteStrategy(java.lang.String name)` Restricted constructor.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `delete(java.io.File fileToDelete)` Deletes the file object, which may be a file or a directory.
    `boolean` | `deleteQuietly(java.io.File fileToDelete)` Deletes the file object, which may be a file or a directory.
    `protected boolean` | `doDelete(java.io.File fileToDelete)` Actually deletes the file object, which may be a file or a directory.
    `java.lang.String` | `toString()` Gets a string describing the delete strategy.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- - ### Field Detail

    - #### NORMAL

          public static final FileDeleteStrategy NORMAL

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

    - #### FORCE

          public static final FileDeleteStrategy FORCE

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

    - #### name

          private final java.lang.String name

      The name of the strategy.

  - ### Constructor Detail

    - #### FileDeleteStrategy

          protected FileDeleteStrategy(java.lang.String name)

      Restricted constructor.
      Parameters:
        `name` - the name by which the strategy is known

  - ### Method Detail

    - #### deleteQuietly

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

      Parameters:
        `fileToDelete` - the file to delete, null returns true
      Returns:
        true if the file was deleted, or there was no such file

    - #### delete

          public void delete(java.io.File fileToDelete)
                      throws java.io.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` - the file to delete, not null
      Throws:
        `java.lang.NullPointerException` - if the file is null
        `java.io.IOException` - if an error occurs during file deletion

    - #### doDelete

          protected boolean doDelete(java.io.File fileToDelete)
                              throws java.io.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` - the file to delete, exists, not null
      Returns:
        true if the file was deleteds
      Throws:
        `java.lang.NullPointerException` - if the file is null
        `java.io.IOException` - if an error occurs during file deletion

    - #### toString

          public java.lang.String toString()

      Gets a string describing the delete strategy.
      Overrides:
        `toString` in class `java.lang.Object`
      Returns:
        a string describing the delete strategy

Copyright (c) 2002-2016 Apache Software Foundation