# Class FileUtils

java.lang.Object
org.apache.tomcat.util.http.fileupload.FileUtils

public class FileUtils extends Object
General file manipulation utilities.

Facilities are provided in the following areas:

- writing to a file
- reading from a file
- make a directory including parent directories
- copying files and directories
- deleting files and directories
- converting to and from a URL
- listing files and directories by filter and extension
- comparing file content
- file last changed date
- calculating a checksum

Note that a specific charset should be specified whenever possible. Relying on the platform default means that the code is Locale-dependent. Only use the default if the files are known to always use the platform default.

Origin of code: Excalibur, Alexandria, Commons-Utils

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `FileUtils()`
  Instances should NOT be constructed in standard programming.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static void`
  `cleanDirectory(File directory)`
  Cleans a directory without deleting it.
  `static void`
  `deleteDirectory(File directory)`
  Deletes a directory recursively.
  `static void`
  `forceDelete(File file)`
  Deletes a file.
  `static void`
  `forceDeleteOnExit(File file)`
  Schedules a file to be deleted when JVM exits.
  `static void`
  `forceMkdir(File directory)`
  Makes a directory, including any necessary but nonexistent parent directories.
  `static void`
  `forceMkdirParent(File file)`
  Makes any necessary but nonexistent parent directories for a given File.
  `static boolean`
  `isSymlink(File file)`
  Determines whether the specified file is a Symbolic Link rather than an actual file.

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

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

- ## Constructor Details

  - ### FileUtils

    public FileUtils()
    Instances should NOT be constructed in standard programming.

- ## Method Details

  - ### deleteDirectory

    public static void deleteDirectory(File directory) throws IOException
    Deletes a directory recursively.
    Parameters:
      `directory` - directory to delete
    Throws:
      `IOException` - in case deletion is unsuccessful
      `IllegalArgumentException` - if `directory` does not exist or is not a directory

  - ### cleanDirectory

    public static void cleanDirectory(File directory) throws IOException
    Cleans a directory without deleting it.
    Parameters:
      `directory` - directory to clean
    Throws:
      `IOException` - in case cleaning is unsuccessful
      `IllegalArgumentException` - if `directory` does not exist or is not a directory

  - ### forceDelete

    public static void forceDelete(File file) throws IOException
    Deletes a file. If file is a directory, delete it and all sub-directories.

    The difference between File.delete() and this method are:

    - A directory to be deleted does not have to be empty.
    - You get exceptions when a file or directory cannot be deleted. (java.io.File methods returns a boolean)
    Parameters:
      `file` - file or directory to delete, must not be `null`
    Throws:
      `NullPointerException` - if the directory is `null`
      `FileNotFoundException` - if the file was not found
      `IOException` - in case deletion is unsuccessful

  - ### forceDeleteOnExit

    public static void forceDeleteOnExit(File file) throws IOException
    Schedules a file to be deleted when JVM exits. If file is directory delete it and all sub-directories.
    Parameters:
      `file` - file or directory to delete, must not be `null`
    Throws:
      `NullPointerException` - if the file is `null`
      `IOException` - in case deletion is unsuccessful

  - ### forceMkdir

    public static void forceMkdir(File directory) throws IOException
    Makes a directory, including any necessary but nonexistent parent directories. If a file already exists with specified name but it is not a directory then an IOException is thrown. If the directory cannot be created (or does not already exist) then an IOException is thrown.
    Parameters:
      `directory` - directory to create, must not be `null`
    Throws:
      `NullPointerException` - if the directory is `null`
      `IOException` - if the directory cannot be created or the file already exists but is not a directory

  - ### forceMkdirParent

    public static void forceMkdirParent(File file) throws IOException
    Makes any necessary but nonexistent parent directories for a given File. If the parent directory cannot be created then an IOException is thrown.
    Parameters:
      `file` - file with parent to create, must not be `null`
    Throws:
      `NullPointerException` - if the file is `null`
      `IOException` - if the parent directory cannot be created
    Since:
      IO 2.5

  - ### isSymlink

    public static boolean isSymlink(File file) throws IOException
    Determines whether the specified file is a Symbolic Link rather than an actual file.

    Will not return true if there is a Symbolic Link anywhere in the path, only if the specific file is.

    Note: the current implementation always returns `false` if the system is detected as Windows using `File.separatorChar` == '\\'

    Parameters:
      `file` - the file to check
    Returns:
      true if the file is a Symbolic Link
    Throws:
      `IOException` - if an IO error occurs while checking the file
    Since:
      IO 2.0