? FileUtils

java.lang.Object
org.apache.commons.io.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.

SecurityException are not documented in the Javadoc.

Origin of code: Excalibur, Alexandria, Commons-Utils

  • ??????

    • ONE_KB

      public static final long ONE_KB
      The number of bytes in a kilobyte.
      ????:
    • ONE_KB_BI

      public static final BigInteger ONE_KB_BI
      The number of bytes in a kilobyte.
      ???????:
      2.4
    • ONE_MB

      public static final long ONE_MB
      The number of bytes in a megabyte.
      ????:
    • ONE_MB_BI

      public static final BigInteger ONE_MB_BI
      The number of bytes in a megabyte.
      ???????:
      2.4
    • ONE_GB

      public static final long ONE_GB
      The number of bytes in a gigabyte.
      ????:
    • ONE_GB_BI

      public static final BigInteger ONE_GB_BI
      The number of bytes in a gigabyte.
      ???????:
      2.4
    • ONE_TB

      public static final long ONE_TB
      The number of bytes in a terabyte.
      ????:
    • ONE_TB_BI

      public static final BigInteger ONE_TB_BI
      The number of bytes in a terabyte.
      ???????:
      2.4
    • ONE_PB

      public static final long ONE_PB
      The number of bytes in a petabyte.
      ????:
    • ONE_PB_BI

      public static final BigInteger ONE_PB_BI
      The number of bytes in a petabyte.
      ???????:
      2.4
    • ONE_EB

      public static final long ONE_EB
      The number of bytes in an exabyte.
      ????:
    • ONE_EB_BI

      public static final BigInteger ONE_EB_BI
      The number of bytes in an exabyte.
      ???????:
      2.4
    • ONE_ZB

      public static final BigInteger ONE_ZB
      The number of bytes in a zettabyte.
    • ONE_YB

      public static final BigInteger ONE_YB
      The number of bytes in a yottabyte.
    • EMPTY_FILE_ARRAY

      public static final File[] EMPTY_FILE_ARRAY
      An empty array of type File.
  • ???????

    • FileUtils

      ????
      Will be private in 3.0.
      Instances should NOT be constructed in standard programming.
  • ??????

    • byteCountToDisplaySize

      Returns a human-readable version of the file size, where the input represents a specific number of bytes.

      If the size is over 1GB, the size is returned as the number of whole GB, i.e. the size is rounded down to the nearest GB boundary.

      Similarly for the 1MB and 1KB boundaries.

      ??:
      size - the number of bytes
      ??:
      a human-readable display value (includes units - EB, PB, TB, GB, MB, KB or bytes)
      ??:
      NullPointerException - if the given BigInteger is null.
      ???????:
      2.4
      ????:
    • byteCountToDisplaySize

      public static String byteCountToDisplaySize(long size)
      Returns a human-readable version of the file size, where the input represents a specific number of bytes.

      If the size is over 1GB, the size is returned as the number of whole GB, i.e. the size is rounded down to the nearest GB boundary.

      Similarly for the 1MB and 1KB boundaries.

      ??:
      size - the number of bytes
      ??:
      a human-readable display value (includes units - EB, PB, TB, GB, MB, KB or bytes)
      ????:
    • checksum

      public static Checksum checksum(File file, Checksum checksum) throws IOException
      Computes the checksum of a file using the specified checksum object. Multiple files may be checked using one Checksum instance if desired simply by reusing the same checksum object. For example:
       long checksum = FileUtils.checksum(file, new CRC32()).getValue();
       
      ??:
      file - the file to checksum, must not be null
      checksum - the checksum object to be used, must not be null
      ??:
      the checksum specified, updated with the content of the file
      ??:
      NullPointerException - if the given File is null.
      NullPointerException - if the given Checksum is null.
      IllegalArgumentException - if the given File does not exist or is not a file.
      IOException - if an IO error occurs reading the file.
      ???????:
      1.3
    • checksumCRC32

      public static long checksumCRC32(File file) throws IOException
      Computes the checksum of a file using the CRC32 checksum routine. The value of the checksum is returned.
      ??:
      file - the file to checksum, must not be null
      ??:
      the checksum value
      ??:
      NullPointerException - if the given File is null.
      IllegalArgumentException - if the given File does not exist or is not a file.
      IOException - if an IO error occurs reading the file.
      ???????:
      1.3
    • cleanDirectory

      public static void cleanDirectory(File directory) throws IOException
      Cleans a directory without deleting it.
      ??:
      directory - directory to clean
      ??:
      NullPointerException - if the given File is null.
      IllegalArgumentException - if directory does not exist or is not a directory.
      IOException - if an I/O error occurs.
      ????:
    • contentEquals

      public static boolean contentEquals(File file1, File file2) throws IOException
      Tests whether the contents of two files are equal.

      This method checks to see if the two files are different lengths or if they point to the same file, before resorting to byte-by-byte comparison of the contents.

      Code origin: Avalon

      ??:
      file1 - the first file
      file2 - the second file
      ??:
      true if the content of the files are equal or they both don't exist, false otherwise
      ??:
      IllegalArgumentException - when an input is not a file.
      IOException - If an I/O error occurs.
      ????:
    • contentEqualsIgnoreEOL

      public static boolean contentEqualsIgnoreEOL(File file1, File file2, String charsetName) throws IOException
      Compares the contents of two files to determine if they are equal or not.

      This method checks to see if the two files point to the same file, before resorting to line-by-line comparison of the contents.

      ??:
      file1 - the first file
      file2 - the second file
      charsetName - the name of the requested charset. May be null, in which case the platform default is used
      ??:
      true if the content of the files are equal or neither exists, false otherwise
      ??:
      IllegalArgumentException - when an input is not a file.
      IOException - in case of an I/O error.
      UnsupportedCharsetException - If the named charset is unavailable (unchecked exception).
      ???????:
      2.2
      ????:
    • convertFileCollectionToFileArray

      Converts a Collection containing java.io.File instanced into array representation. This is to account for the difference between File.listFiles() and FileUtils.listFiles().
      ??:
      files - a Collection containing java.io.File instances
      ??:
      an array of java.io.File
    • copyDirectory

      public static void copyDirectory(File srcDir, File destDir) throws IOException
      Copies a whole directory to a new location preserving the file dates.

      This method copies the specified directory and all its child directories and files to the specified destination. The destination is the new location and name of the directory.

      The destination directory is created if it does not exist. If the destination directory did exist, then this method merges the source with the destination, with the source taking precedence.

      Note: This method tries to preserve the files' last modified date/times using File.setLastModified(long), however it is not guaranteed that those operations will succeed. If the modification operation fails, the methods throws IOException.

      ??:
      srcDir - an existing directory to copy, must not be null.
      destDir - the new directory, must not be null.
      ??:
      NullPointerException - if any of the given Files are null.
      IllegalArgumentException - if the source or destination is invalid.
      FileNotFoundException - if the source does not exist.
      IOException - if an error occurs or setting the last-modified time didn't succeeded.
      ???????:
      1.1
    • copyDirectory

      public static void copyDirectory(File srcDir, File destDir, boolean preserveFileDate) throws IOException
      Copies a whole directory to a new location.

      This method copies the contents of the specified source directory to within the specified destination directory.

      The destination directory is created if it does not exist. If the destination directory did exist, then this method merges the source with the destination, with the source taking precedence.

      Note: Setting preserveFileDate to true tries to preserve the files' last modified date/times using File.setLastModified(long), however it is not guaranteed that those operations will succeed. If the modification operation fails, the methods throws IOException.

      ??:
      srcDir - an existing directory to copy, must not be null.
      destDir - the new directory, must not be null.
      preserveFileDate - true if the file date of the copy should be the same as the original.
      ??:
      NullPointerException - if any of the given Files are null.
      IllegalArgumentException - if the source or destination is invalid.
      FileNotFoundException - if the source does not exist.
      IOException - if an error occurs or setting the last-modified time didn't succeeded.
      ???????:
      1.1
    • copyDirectory

      public static void copyDirectory(File srcDir, File destDir, FileFilter filter) throws IOException
      Copies a filtered directory to a new location preserving the file dates.

      This method copies the contents of the specified source directory to within the specified destination directory.

      The destination directory is created if it does not exist. If the destination directory did exist, then this method merges the source with the destination, with the source taking precedence.

      Note: This method tries to preserve the files' last modified date/times using File.setLastModified(long), however it is not guaranteed that those operations will succeed. If the modification operation fails, the methods throws IOException.

      Example: Copy directories only
       // only copy the directory structure
       FileUtils.copyDirectory(srcDir, destDir, DirectoryFileFilter.DIRECTORY);
       
      Example: Copy directories and txt files
       // Create a filter for ".txt" files
       IOFileFilter txtSuffixFilter = FileFilterUtils.suffixFileFilter(".txt");
       IOFileFilter txtFiles = FileFilterUtils.andFileFilter(FileFileFilter.FILE, txtSuffixFilter);
      
       // Create a filter for either directories or ".txt" files
       FileFilter filter = FileFilterUtils.orFileFilter(DirectoryFileFilter.DIRECTORY, txtFiles);
      
       // Copy using the filter
       FileUtils.copyDirectory(srcDir, destDir, filter);
       
      ??:
      srcDir - an existing directory to copy, must not be null.
      destDir - the new directory, must not be null.
      filter - the filter to apply, null means copy all directories and files should be the same as the original.
      ??:
      NullPointerException - if any of the given Files are null.
      IllegalArgumentException - if the source or destination is invalid.
      FileNotFoundException - if the source does not exist.
      IOException - if an error occurs or setting the last-modified time didn't succeeded.
      ???????:
      1.4
    • copyDirectory

      public static void copyDirectory(File srcDir, File destDir, FileFilter filter, boolean preserveFileDate) throws IOException
      Copies a filtered directory to a new location.

      This method copies the contents of the specified source directory to within the specified destination directory.

      The destination directory is created if it does not exist. If the destination directory did exist, then this method merges the source with the destination, with the source taking precedence.

      Note: Setting preserveFileDate to true tries to preserve the files' last modified date/times using File.setLastModified(long), however it is not guaranteed that those operations will succeed. If the modification operation fails, the methods throws IOException.

      Example: Copy directories only
       // only copy the directory structure
       FileUtils.copyDirectory(srcDir, destDir, DirectoryFileFilter.DIRECTORY, false);
       
      Example: Copy directories and txt files
       // Create a filter for ".txt" files
       IOFileFilter txtSuffixFilter = FileFilterUtils.suffixFileFilter(".txt");
       IOFileFilter txtFiles = FileFilterUtils.andFileFilter(FileFileFilter.FILE, txtSuffixFilter);
      
       // Create a filter for either directories or ".txt" files
       FileFilter filter = FileFilterUtils.orFileFilter(DirectoryFileFilter.DIRECTORY, txtFiles);
      
       // Copy using the filter
       FileUtils.copyDirectory(srcDir, destDir, filter, false);
       
      ??:
      srcDir - an existing directory to copy, must not be null.
      destDir - the new directory, must not be null.
      filter - the filter to apply, null means copy all directories and files.
      preserveFileDate - true if the file date of the copy should be the same as the original.
      ??:
      NullPointerException - if any of the given Files are null.
      IllegalArgumentException - if the source or destination is invalid.
      FileNotFoundException - if the source does not exist.
      IOException - if an error occurs or setting the last-modified time didn't succeeded.
      ???????:
      1.4
    • copyDirectory

      public static void copyDirectory(File srcDir, File destDir, FileFilter fileFilter, boolean preserveFileDate, CopyOption... copyOptions) throws IOException
      Copies a filtered directory to a new location.

      This method copies the contents of the specified source directory to within the specified destination directory.

      The destination directory is created if it does not exist. If the destination directory did exist, then this method merges the source with the destination, with the source taking precedence.

      Note: Setting preserveFileDate to true tries to preserve the files' last modified date/times using File.setLastModified(long), however it is not guaranteed that those operations will succeed. If the modification operation fails, the methods throws IOException.

      Example: Copy directories only
       // only copy the directory structure
       FileUtils.copyDirectory(srcDir, destDir, DirectoryFileFilter.DIRECTORY, false);
       
      Example: Copy directories and txt files
       // Create a filter for ".txt" files
       IOFileFilter txtSuffixFilter = FileFilterUtils.suffixFileFilter(".txt");
       IOFileFilter txtFiles = FileFilterUtils.andFileFilter(FileFileFilter.FILE, txtSuffixFilter);
      
       // Create a filter for either directories or ".txt" files
       FileFilter filter = FileFilterUtils.orFileFilter(DirectoryFileFilter.DIRECTORY, txtFiles);
      
       // Copy using the filter
       FileUtils.copyDirectory(srcDir, destDir, filter, false);
       
      ??:
      srcDir - an existing directory to copy, must not be null
      destDir - the new directory, must not be null
      fileFilter - the filter to apply, null means copy all directories and files
      preserveFileDate - true if the file date of the copy should be the same as the original
      copyOptions - options specifying how the copy should be done, for example StandardCopyOption.
      ??:
      NullPointerException - if any of the given Files are null.
      IllegalArgumentException - if the source or destination is invalid.
      FileNotFoundException - if the source does not exist.
      IOException - if an error occurs or setting the last-modified time didn't succeeded.
      ???????:
      2.8.0
    • copyDirectoryToDirectory

      public static void copyDirectoryToDirectory(File sourceDir, File destinationDir) throws IOException
      Copies a directory to within another directory preserving the file dates.

      This method copies the source directory and all its contents to a directory of the same name in the specified destination directory.

      The destination directory is created if it does not exist. If the destination directory did exist, then this method merges the source with the destination, with the source taking precedence.

      Note: This method tries to preserve the files' last modified date/times using File.setLastModified(long), however it is not guaranteed that those operations will succeed. If the modification operation fails, the methods throws IOException.

      ??:
      sourceDir - an existing directory to copy, must not be null.
      destinationDir - the directory to place the copy in, must not be null.
      ??:
      NullPointerException - if any of the given Files are null.
      IllegalArgumentException - if the source or destination is invalid.
      FileNotFoundException - if the source does not exist.
      IOException - if an error occurs or setting the last-modified time didn't succeeded.
      ???????:
      1.2
    • copyFile

      public static void copyFile(File srcFile, File destFile) throws IOException
      Copies a file to a new location preserving the file date.

      This method copies the contents of the specified source file to the specified destination file. The directory holding the destination file is created if it does not exist. If the destination file exists, then this method will overwrite it.

      Note: This method tries to preserve the file's last modified date/times using File.setLastModified(long), however it is not guaranteed that the operation will succeed. If the modification operation fails, the methods throws IOException.

      ??:
      srcFile - an existing file to copy, must not be null.
      destFile - the new file, must not be null.
      ??:
      NullPointerException - if any of the given Files are null.
      IOException - if source or destination is invalid.
      IOException - if an error occurs or setting the last-modified time didn't succeeded.
      IOException - if the output file length is not the same as the input file length after the copy completes.
      ????:
    • copyFile

      public static void copyFile(File srcFile, File destFile, boolean preserveFileDate) throws IOException
      Copies an existing file to a new file location.

      This method copies the contents of the specified source file to the specified destination file. The directory holding the destination file is created if it does not exist. If the destination file exists, then this method will overwrite it.

      Note: Setting preserveFileDate to true tries to preserve the file's last modified date/times using File.setLastModified(long), however it is not guaranteed that the operation will succeed. If the modification operation fails, the methods throws IOException.

      ??:
      srcFile - an existing file to copy, must not be null.
      destFile - the new file, must not be null.
      preserveFileDate - true if the file date of the copy should be the same as the original.
      ??:
      NullPointerException - if any of the given Files are null.
      IOException - if source or destination is invalid.
      IOException - if an error occurs or setting the last-modified time didn't succeeded.
      IOException - if the output file length is not the same as the input file length after the copy completes
      ????:
    • copyFile

      public static void copyFile(File srcFile, File destFile, boolean preserveFileDate, CopyOption... copyOptions) throws IOException
      Copies a file to a new location.

      This method copies the contents of the specified source file to the specified destination file. The directory holding the destination file is created if it does not exist. If the destination file exists, you can overwrite it with StandardCopyOption.REPLACE_EXISTING.

      Note: Setting preserveFileDate to true tries to preserve the file's last modified date/times using File.setLastModified(long), however it is not guaranteed that the operation will succeed. If the modification operation fails, the methods throws IOException.

      ??:
      srcFile - an existing file to copy, must not be null.
      destFile - the new file, must not be null.
      preserveFileDate - true if the file date of the copy should be the same as the original.
      copyOptions - options specifying how the copy should be done, for example StandardCopyOption..
      ??:
      NullPointerException - if any of the given Files are null.
      FileNotFoundException - if the source does not exist.
      IllegalArgumentException - if source is not a file.
      IOException - if the output file length is not the same as the input file length after the copy completes.
      IOException - if an I/O error occurs, or setting the last-modified time didn't succeeded.
      ???????:
      2.8.0
      ????:
    • copyFile

      public static void copyFile(File srcFile, File destFile, CopyOption... copyOptions) throws IOException
      Copies a file to a new location.

      This method copies the contents of the specified source file to the specified destination file. The directory holding the destination file is created if it does not exist. If the destination file exists, you can overwrite it if you use StandardCopyOption.REPLACE_EXISTING.

      ??:
      srcFile - an existing file to copy, must not be null.
      destFile - the new file, must not be null.
      copyOptions - options specifying how the copy should be done, for example StandardCopyOption..
      ??:
      NullPointerException - if any of the given Files are null.
      FileNotFoundException - if the source does not exist.
      IllegalArgumentException - if source is not a file.
      IOException - if the output file length is not the same as the input file length after the copy completes.
      IOException - if an I/O error occurs.
      ???????:
      2.9.0
      ????:
    • copyFile

      public static long copyFile(File input, OutputStream output) throws IOException
      Copies bytes from a File to an OutputStream.

      This method buffers the input internally, so there is no need to use a BufferedInputStream.

      ??:
      input - the File to read.
      output - the OutputStream to write.
      ??:
      the number of bytes copied
      ??:
      NullPointerException - if the File is null.
      NullPointerException - if the OutputStream is null.
      IOException - if an I/O error occurs.
      ???????:
      2.1
    • copyFileToDirectory

      public static void copyFileToDirectory(File srcFile, File destDir) throws IOException
      Copies a file to a directory preserving the file date.

      This method copies the contents of the specified source file to a file of the same name in the specified destination directory. The destination directory is created if it does not exist. If the destination file exists, then this method will overwrite it.

      Note: This method tries to preserve the file's last modified date/times using File.setLastModified(long), however it is not guaranteed that the operation will succeed. If the modification operation fails, the methods throws IOException.

      ??:
      srcFile - an existing file to copy, must not be null.
      destDir - the directory to place the copy in, must not be null.
      ??:
      NullPointerException - if any of the given Files are null.
      IllegalArgumentException - if source or destination is invalid.
      IOException - if an error occurs or setting the last-modified time didn't succeeded.
      ????:
    • copyFileToDirectory

      public static void copyFileToDirectory(File sourceFile, File destinationDir, boolean preserveFileDate) throws IOException
      Copies a file to a directory optionally preserving the file date.

      This method copies the contents of the specified source file to a file of the same name in the specified destination directory. The destination directory is created if it does not exist. If the destination file exists, then this method will overwrite it.

      Note: Setting preserveFileDate to true tries to preserve the file's last modified date/times using File.setLastModified(long), however it is not guaranteed that the operation will succeed. If the modification operation fails, the methods throws IOException.

      ??:
      sourceFile - an existing file to copy, must not be null.
      destinationDir - the directory to place the copy in, must not be null.
      preserveFileDate - true if the file date of the copy should be the same as the original.
      ??:
      NullPointerException - if any of the given Files are null.
      IOException - if an error occurs or setting the last-modified time didn't succeeded.
      IOException - if the output file length is not the same as the input file length after the copy completes.
      ???????:
      1.3
      ????:
    • copyInputStreamToFile

      public static void copyInputStreamToFile(InputStream source, File destination) throws IOException
      Copies bytes from an InputStream source to a file destination. The directories up to destination will be created if they don't already exist. destination will be overwritten if it already exists.

      The source stream is closed.

      See copyToFile(InputStream, File) for a method that does not close the input stream.

      ??:
      source - the InputStream to copy bytes from, must not be null, will be closed
      destination - the non-directory File to write bytes to (possibly overwriting), must not be null
      ??:
      IOException - if destination is a directory
      IOException - if destination cannot be written
      IOException - if destination needs creating but can't be
      IOException - if an IO error occurs during copying
      ???????:
      2.0
    • copyToDirectory

      public static void copyToDirectory(File sourceFile, File destinationDir) throws IOException
      Copies a file or directory to within another directory preserving the file dates.

      This method copies the source file or directory, along all its contents, to a directory of the same name in the specified destination directory.

      The destination directory is created if it does not exist. If the destination directory did exist, then this method merges the source with the destination, with the source taking precedence.

      Note: This method tries to preserve the files' last modified date/times using File.setLastModified(long), however it is not guaranteed that those operations will succeed. If the modification operation fails, the methods throws IOException.

      ??:
      sourceFile - an existing file or directory to copy, must not be null.
      destinationDir - the directory to place the copy in, must not be null.
      ??:
      NullPointerException - if any of the given Files are null.
      IllegalArgumentException - if the source or destination is invalid.
      FileNotFoundException - if the source does not exist.
      IOException - if an error occurs or setting the last-modified time didn't succeeded.
      ???????:
      2.6
      ????:
    • copyToDirectory

      public static void copyToDirectory(Iterable<File> sourceIterable, File destinationDir) throws IOException
      Copies a files to a directory preserving each file's date.

      This method copies the contents of the specified source files to a file of the same name in the specified destination directory. The destination directory is created if it does not exist. If the destination file exists, then this method will overwrite it.

      Note: This method tries to preserve the file's last modified date/times using File.setLastModified(long), however it is not guaranteed that the operation will succeed. If the modification operation fails, the methods throws IOException.

      ??:
      sourceIterable - a existing files to copy, must not be null.
      destinationDir - the directory to place the copy in, must not be null.
      ??:
      NullPointerException - if any of the given Files are null.
      IOException - if source or destination is invalid.
      IOException - if an error occurs or setting the last-modified time didn't succeeded.
      ???????:
      2.6
      ????:
    • copyToFile

      public static void copyToFile(InputStream inputStream, File file) throws IOException
      Copies bytes from an InputStream source to a File destination. The directories up to destination will be created if they don't already exist. destination will be overwritten if it already exists. The source stream is left open, e.g. for use with ZipInputStream. See copyInputStreamToFile(InputStream, File) for a method that closes the input stream.
      ??:
      inputStream - the InputStream to copy bytes from, must not be null
      file - the non-directory File to write bytes to (possibly overwriting), must not be null
      ??:
      NullPointerException - if the InputStream is null.
      NullPointerException - if the File is null.
      IllegalArgumentException - if the file object is a directory.
      IllegalArgumentException - if the file is not writable.
      IOException - if the directories could not be created.
      IOException - if an IO error occurs during copying.
      ???????:
      2.5
    • copyURLToFile

      public static void copyURLToFile(URL source, File destination) throws IOException
      Copies bytes from the URL source to a file destination. The directories up to destination will be created if they don't already exist. destination will be overwritten if it already exists.

      Warning: this method does not set a connection or read timeout and thus might block forever. Use copyURLToFile(URL, File, int, int) with reasonable timeouts to prevent this.

      ??:
      source - the URL to copy bytes from, must not be null
      destination - the non-directory File to write bytes to (possibly overwriting), must not be null
      ??:
      IOException - if source URL cannot be opened
      IOException - if destination is a directory
      IOException - if destination cannot be written
      IOException - if destination needs creating but can't be
      IOException - if an IO error occurs during copying
    • copyURLToFile

      public static void copyURLToFile(URL source, File destination, int connectionTimeoutMillis, int readTimeoutMillis) throws IOException
      Copies bytes from the URL source to a file destination. The directories up to destination will be created if they don't already exist. destination will be overwritten if it already exists.
      ??:
      source - the URL to copy bytes from, must not be null
      destination - the non-directory File to write bytes to (possibly overwriting), must not be null
      connectionTimeoutMillis - the number of milliseconds until this method will timeout if no connection could be established to the source
      readTimeoutMillis - the number of milliseconds until this method will timeout if no data could be read from the source
      ??:
      IOException - if source URL cannot be opened
      IOException - if destination is a directory
      IOException - if destination cannot be written
      IOException - if destination needs creating but can't be
      IOException - if an IO error occurs during copying
      ???????:
      2.0
    • createParentDirectories

      public static File createParentDirectories(File file) throws IOException
      Creates all parent directories for a File object.
      ??:
      file - the File that may need parents, may be null.
      ??:
      The parent directory, or null if the given file does not name a parent
      ??:
      IOException - if the directory was not created along with all its parent directories.
      IOException - if the given file object is not null and not a directory.
      ???????:
      2.9.0
    • delete

      public static File delete(File file) throws IOException
      Deletes the given File but throws an IOException if it cannot, unlike File.delete() which returns a boolean.
      ??:
      file - The file to delete.
      ??:
      the given file.
      ??:
      IOException - if the file cannot be deleted.
      ???????:
      2.9.0
      ????:
    • deleteDirectory

      public static void deleteDirectory(File directory) throws IOException
      Deletes a directory recursively.
      ??:
      directory - directory to delete
      ??:
      IOException - in case deletion is unsuccessful
      IllegalArgumentException - if directory is not a directory
    • deleteQuietly

      public static boolean deleteQuietly(File file)
      Deletes a file, never throwing an exception. 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.
      • No exceptions are thrown when a file or directory cannot be deleted.
      ??:
      file - file or directory to delete, can be null
      ??:
      true if the file or directory was deleted, otherwise false
      ???????:
      1.4
    • directoryContains

      public static boolean directoryContains(File directory, File child) throws IOException
      Determines whether the parent directory contains the child element (a file or directory).

      Files are normalized before comparison.

      Edge cases:
      • A directory must not be null: if null, throw IllegalArgumentException
      • A directory must be a directory: if not a directory, throw IllegalArgumentException
      • A directory does not contain itself: return false
      • A null child file is not contained in any parent: return false
      ??:
      directory - the file to consider as the parent.
      child - the file to consider as the child.
      ??:
      true is the candidate leaf is under by the specified composite. False otherwise.
      ??:
      IOException - if an IO error occurs while checking the files.
      NullPointerException - if the given File is null.
      IllegalArgumentException - if the given File does not exist or is not a directory.
      ???????:
      2.2
      ????:
    • forceDelete

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

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

      • The directory does not have to be empty.
      • You get an exception when a file or directory cannot be deleted.
      ??:
      file - file or directory to delete, must not be null.
      ??:
      NullPointerException - if the file 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.
      ??:
      file - file or directory to delete, must not be null.
      ??:
      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 the file already exists but is not a directory) then an IOException is thrown.
      ??:
      directory - directory to create, must not be null.
      ??:
      IOException - if the directory was not created along with all its parent directories.
      IOException - if the given file object is not a directory.
      SecurityException - See File.mkdirs().
    • 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.
      ??:
      file - file with parent to create, must not be null.
      ??:
      NullPointerException - if the file is null.
      IOException - if the parent directory cannot be created.
      ???????:
      2.5
    • getFile

      public static File getFile(File directory, String... names)
      Construct a file from the set of name elements.
      ??:
      directory - the parent directory.
      names - the name elements.
      ??:
      the new file.
      ???????:
      2.1
    • getFile

      public static File getFile(String... names)
      Construct a file from the set of name elements.
      ??:
      names - the name elements.
      ??:
      the file.
      ???????:
      2.1
    • getTempDirectory

      public static File getTempDirectory()
      Returns a File representing the system temporary directory.
      ??:
      the system temporary directory.
      ???????:
      2.0
    • getTempDirectoryPath

      public static String getTempDirectoryPath()
      Returns the path to the system temporary directory.
      ??:
      the path to the system temporary directory.
      ???????:
      2.0
    • getUserDirectory

      public static File getUserDirectory()
      Returns a File representing the user's home directory.
      ??:
      the user's home directory.
      ???????:
      2.0
    • getUserDirectoryPath

      public static String getUserDirectoryPath()
      Returns the path to the user's home directory.
      ??:
      the path to the user's home directory.
      ???????:
      2.0
    • isDirectory

      public static boolean isDirectory(File file, LinkOption... options)
      Tests whether the specified File is a directory or not. Implemented as a null-safe delegate to Files.isDirectory(Path path, LinkOption... options).
      ??:
      file - the path to the file.
      options - options indicating how symbolic links are handled
      ??:
      true if the file is a directory; false if the path is null, the file does not exist, is not a directory, or it cannot be determined if the file is a directory or not.
      ??:
      SecurityException - In the case of the default provider, and a security manager is installed, the checkRead method is invoked to check read access to the directory.
      ???????:
      2.9.0
    • isEmptyDirectory

      public static boolean isEmptyDirectory(File directory) throws IOException
      Tests whether the directory is empty.
      ??:
      directory - the directory to query.
      ??:
      whether the directory is empty.
      ??:
      IOException - if an I/O error occurs.
      NotDirectoryException - if the file could not otherwise be opened because it is not a directory (optional specific exception).
      ???????:
      2.9.0
    • isFileNewer

      public static boolean isFileNewer(File file, ChronoLocalDate chronoLocalDate)
      Tests if the specified File is newer than the specified ChronoLocalDate at the current time.

      Note: The input date is assumed to be in the system default time-zone with the time part set to the current time. To use a non-default time-zone use the method isFileNewer(file, chronoLocalDate.atTime(LocalTime.now(zoneId)), zoneId) where zoneId is a valid ZoneId.

      ??:
      file - the File of which the modification date must be compared.
      chronoLocalDate - the date reference.
      ??:
      true if the File exists and has been modified after the given ChronoLocalDate at the current time.
      ??:
      NullPointerException - if the file or local date is null.
      ???????:
      2.8.0
    • isFileNewer

      public static boolean isFileNewer(File file, ChronoLocalDate chronoLocalDate, LocalTime localTime)
      Tests if the specified File is newer than the specified ChronoLocalDate at the specified time.

      Note: The input date and time are assumed to be in the system default time-zone. To use a non-default time-zone use the method isFileNewer(file, chronoLocalDate.atTime(localTime), zoneId) where zoneId is a valid ZoneId.

      ??:
      file - the File of which the modification date must be compared.
      chronoLocalDate - the date reference.
      localTime - the time reference.
      ??:
      true if the File exists and has been modified after the given ChronoLocalDate at the given time.
      ??:
      NullPointerException - if the file, local date or zone ID is null.
      ???????:
      2.8.0
    • isFileNewer

      public static boolean isFileNewer(File file, ChronoLocalDateTime<?> chronoLocalDateTime)
      Tests if the specified File is newer than the specified ChronoLocalDateTime at the system-default time zone.

      Note: The input date and time is assumed to be in the system default time-zone. To use a non-default time-zone use the method isFileNewer(file, chronoLocalDateTime, zoneId) where zoneId is a valid ZoneId.

      ??:
      file - the File of which the modification date must be compared.
      chronoLocalDateTime - the date reference.
      ??:
      true if the File exists and has been modified after the given ChronoLocalDateTime at the system-default time zone.
      ??:
      NullPointerException - if the file or local date time is null.
      ???????:
      2.8.0
    • isFileNewer

      public static boolean isFileNewer(File file, ChronoLocalDateTime<?> chronoLocalDateTime, ZoneId zoneId)
      Tests if the specified File is newer than the specified ChronoLocalDateTime at the specified ZoneId.
      ??:
      file - the File of which the modification date must be compared.
      chronoLocalDateTime - the date reference.
      zoneId - the time zone.
      ??:
      true if the File exists and has been modified after the given ChronoLocalDateTime at the given ZoneId.
      ??:
      NullPointerException - if the file, local date time or zone ID is null.
      ???????:
      2.8.0
    • isFileNewer

      public static boolean isFileNewer(File file, ChronoZonedDateTime<?> chronoZonedDateTime)
      Tests if the specified File is newer than the specified ChronoZonedDateTime.
      ??:
      file - the File of which the modification date must be compared.
      chronoZonedDateTime - the date reference.
      ??:
      true if the File exists and has been modified after the given ChronoZonedDateTime.
      ??:
      NullPointerException - if the file or zoned date time is null.
      ???????:
      2.8.0
    • isFileNewer

      public static boolean isFileNewer(File file, Date date)
      Tests if the specified File is newer than the specified Date.
      ??:
      file - the File of which the modification date must be compared.
      date - the date reference.
      ??:
      true if the File exists and has been modified after the given Date.
      ??:
      NullPointerException - if the file or date is null.
    • isFileNewer

      public static boolean isFileNewer(File file, File reference)
      Tests if the specified File is newer than the reference File.
      ??:
      file - the File of which the modification date must be compared.
      reference - the File of which the modification date is used.
      ??:
      true if the File exists and has been modified more recently than the reference File.
      ??:
      NullPointerException - if the file or reference file is null.
      IllegalArgumentException - if the reference file doesn't exist.
    • isFileNewer

      public static boolean isFileNewer(File file, Instant instant)
      Tests if the specified File is newer than the specified Instant.
      ??:
      file - the File of which the modification date must be compared.
      instant - the date reference.
      ??:
      true if the File exists and has been modified after the given Instant.
      ??:
      NullPointerException - if the file or instant is null.
      ???????:
      2.8.0
    • isFileNewer

      public static boolean isFileNewer(File file, long timeMillis)
      Tests if the specified File is newer than the specified time reference.
      ??:
      file - the File of which the modification date must be compared.
      timeMillis - the time reference measured in milliseconds since the epoch (00:00:00 GMT, January 1, 1970).
      ??:
      true if the File exists and has been modified after the given time reference.
      ??:
      NullPointerException - if the file is null.
    • isFileOlder

      public static boolean isFileOlder(File file, ChronoLocalDate chronoLocalDate)
      Tests if the specified File is older than the specified ChronoLocalDate at the current time.

      Note: The input date is assumed to be in the system default time-zone with the time part set to the current time. To use a non-default time-zone use the method isFileOlder(file, chronoLocalDate.atTime(LocalTime.now(zoneId)), zoneId) where zoneId is a valid ZoneId.

      ??:
      file - the File of which the modification date must be compared.
      chronoLocalDate - the date reference.
      ??:
      true if the File exists and has been modified before the given ChronoLocalDate at the current time.
      ??:
      NullPointerException - if the file or local date is null.
      ???????:
      2.8.0
      ????:
    • isFileOlder

      public static boolean isFileOlder(File file, ChronoLocalDate chronoLocalDate, LocalTime localTime)
      Tests if the specified File is older than the specified ChronoLocalDate at the specified LocalTime.

      Note: The input date and time are assumed to be in the system default time-zone. To use a non-default time-zone use the method isFileOlder(file, chronoLocalDate.atTime(localTime), zoneId) where zoneId is a valid ZoneId.

      ??:
      file - the File of which the modification date must be compared.
      chronoLocalDate - the date reference.
      localTime - the time reference.
      ??:
      true if the File exists and has been modified before the given ChronoLocalDate at the specified time.
      ??:
      NullPointerException - if the file, local date or local time is null.
      ???????:
      2.8.0
      ????:
    • isFileOlder

      public static boolean isFileOlder(File file, ChronoLocalDateTime<?> chronoLocalDateTime)
      Tests if the specified File is older than the specified ChronoLocalDateTime at the system-default time zone.

      Note: The input date and time is assumed to be in the system default time-zone. To use a non-default time-zone use the method isFileOlder(file, chronoLocalDateTime, zoneId) where zoneId is a valid ZoneId.

      ??:
      file - the File of which the modification date must be compared.
      chronoLocalDateTime - the date reference.
      ??:
      true if the File exists and has been modified before the given ChronoLocalDateTime at the system-default time zone.
      ??:
      NullPointerException - if the file or local date time is null.
      ???????:
      2.8.0
      ????:
    • isFileOlder

      public static boolean isFileOlder(File file, ChronoLocalDateTime<?> chronoLocalDateTime, ZoneId zoneId)
      Tests if the specified File is older than the specified ChronoLocalDateTime at the specified ZoneId.
      ??:
      file - the File of which the modification date must be compared.
      chronoLocalDateTime - the date reference.
      zoneId - the time zone.
      ??:
      true if the File exists and has been modified before the given ChronoLocalDateTime at the given ZoneId.
      ??:
      NullPointerException - if the file, local date time or zone ID is null.
      ???????:
      2.8.0
    • isFileOlder

      public static boolean isFileOlder(File file, ChronoZonedDateTime<?> chronoZonedDateTime)
      Tests if the specified File is older than the specified ChronoZonedDateTime.
      ??:
      file - the File of which the modification date must be compared.
      chronoZonedDateTime - the date reference.
      ??:
      true if the File exists and has been modified before the given ChronoZonedDateTime.
      ??:
      NullPointerException - if the file or zoned date time is null.
      ???????:
      2.8.0
    • isFileOlder

      public static boolean isFileOlder(File file, Date date)
      Tests if the specified File is older than the specified Date.
      ??:
      file - the File of which the modification date must be compared.
      date - the date reference.
      ??:
      true if the File exists and has been modified before the given Date.
      ??:
      NullPointerException - if the file or date is null.
    • isFileOlder

      public static boolean isFileOlder(File file, File reference)
      Tests if the specified File is older than the reference File.
      ??:
      file - the File of which the modification date must be compared.
      reference - the File of which the modification date is used.
      ??:
      true if the File exists and has been modified before the reference File.
      ??:
      NullPointerException - if the file or reference file is null.
      IllegalArgumentException - if the reference file doesn't exist.
    • isFileOlder

      public static boolean isFileOlder(File file, Instant instant)
      Tests if the specified File is older than the specified Instant.
      ??:
      file - the File of which the modification date must be compared.
      instant - the date reference.
      ??:
      true if the File exists and has been modified before the given Instant.
      ??:
      NullPointerException - if the file or instant is null.
      ???????:
      2.8.0
    • isFileOlder

      public static boolean isFileOlder(File file, long timeMillis)
      Tests if the specified File is older than the specified time reference.
      ??:
      file - the File of which the modification date must be compared.
      timeMillis - the time reference measured in milliseconds since the epoch (00:00:00 GMT, January 1, 1970).
      ??:
      true if the File exists and has been modified before the given time reference.
      ??:
      NullPointerException - if the file is null.
    • isRegularFile

      public static boolean isRegularFile(File file, LinkOption... options)
      Tests whether the specified File is a regular file or not. Implemented as a null-safe delegate to Files.isRegularFile(Path path, LinkOption... options).
      ??:
      file - the path to the file.
      options - options indicating how symbolic links are handled
      ??:
      true if the file is a regular file; false if the path is null, the file does not exist, is not a directory, or it cannot be determined if the file is a regular file or not.
      ??:
      SecurityException - In the case of the default provider, and a security manager is installed, the checkRead method is invoked to check read access to the directory.
      ???????:
      2.9.0
    • isSymlink

      public static boolean isSymlink(File file)
      Tests whether the specified file is a symbolic link rather than an actual file.

      This method delegates to Files.isSymbolicLink(Path path)

      ??:
      file - the file to test.
      ??:
      true if the file is a symbolic link, see Files.isSymbolicLink(Path path).
      ???????:
      2.0
      ????:
    • iterateFiles

      public static Iterator<File> iterateFiles(File directory, IOFileFilter fileFilter, IOFileFilter dirFilter)
      Iterates over the files in given directory (and optionally its subdirectories).

      The resulting iterator MUST be consumed in its entirety in order to close its underlying stream.

      All files found are filtered by an IOFileFilter.

      ??:
      directory - the directory to search in
      fileFilter - filter to apply when finding files.
      dirFilter - optional filter to apply when finding subdirectories. If this parameter is null, subdirectories will not be included in the search. Use TrueFileFilter.INSTANCE to match all directories.
      ??:
      an iterator of java.io.File for the matching files
      ???????:
      1.2
      ????:
    • iterateFiles

      public static Iterator<File> iterateFiles(File directory, String[] extensions, boolean recursive)
      Iterates over the files in a given directory (and optionally its subdirectories) which match an array of extensions.

      The resulting iterator MUST be consumed in its entirety in order to close its underlying stream.

      ??:
      directory - the directory to search in
      extensions - an array of extensions, ex. {"java","xml"}. If this parameter is null, all files are returned.
      recursive - if true all subdirectories are searched as well
      ??:
      an iterator of java.io.File with the matching files
      ???????:
      1.2
    • iterateFilesAndDirs

      public static Iterator<File> iterateFilesAndDirs(File directory, IOFileFilter fileFilter, IOFileFilter dirFilter)
      Iterates over the files in given directory (and optionally its subdirectories).

      The resulting iterator MUST be consumed in its entirety in order to close its underlying stream.

      All files found are filtered by an IOFileFilter.

      The resulting iterator includes the subdirectories themselves.

      ??:
      directory - the directory to search in
      fileFilter - filter to apply when finding files.
      dirFilter - optional filter to apply when finding subdirectories. If this parameter is null, subdirectories will not be included in the search. Use TrueFileFilter.INSTANCE to match all directories.
      ??:
      an iterator of java.io.File for the matching files
      ???????:
      2.2
      ????:
    • lastModified

      public static long lastModified(File file) throws IOException
      Returns the last modification time in milliseconds via Files.getLastModifiedTime(Path, LinkOption...).

      Use this method to avoid issues with File.lastModified() like JDK-8177809 where File.lastModified() is losing milliseconds (always ends in 000). This bug exists in OpenJDK 8 and 9, and is fixed in 10.

      ??:
      file - The File to query.
      ??:
      See FileTime.toMillis().
      ??:
      IOException - if an I/O error occurs.
      ???????:
      2.9.0
    • lastModifiedUnchecked

      public static long lastModifiedUnchecked(File file)
      Returns the last modification time in milliseconds via Files.getLastModifiedTime(Path, LinkOption...).

      Use this method to avoid issues with File.lastModified() like JDK-8177809 where File.lastModified() is losing milliseconds (always ends in 000). This bug exists in OpenJDK 8 and 9, and is fixed in 10.

      ??:
      file - The File to query.
      ??:
      See FileTime.toMillis().
      ??:
      UncheckedIOException - if an I/O error occurs.
      ???????:
      2.9.0
    • lineIterator

      public static LineIterator lineIterator(File file) throws IOException
      Returns an Iterator for the lines in a File using the default encoding for the VM.
      ??:
      file - the file to open for input, must not be null
      ??:
      an Iterator of the lines in the file, never null
      ??:
      NullPointerException - if file is null.
      FileNotFoundException - if the file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading.
      IOException - if an I/O error occurs.
      ???????:
      1.3
      ????:
    • lineIterator

      public static LineIterator lineIterator(File file, String charsetName) throws IOException
      Returns an Iterator for the lines in a File.

      This method opens an InputStream for the file. When you have finished with the iterator you should close the stream to free internal resources. This can be done by calling the LineIterator.close() or LineIterator.closeQuietly(LineIterator) method.

      The recommended usage pattern is:

       LineIterator it = FileUtils.lineIterator(file, "UTF-8");
       try {
         while (it.hasNext()) {
           String line = it.nextLine();
           /// do something with line
         }
       } finally {
         LineIterator.closeQuietly(iterator);
       }
       

      If an exception occurs during the creation of the iterator, the underlying stream is closed.

      ??:
      file - the file to open for input, must not be null
      charsetName - the name of the requested charset, null means platform default
      ??:
      an Iterator of the lines in the file, never null
      ??:
      NullPointerException - if file is null.
      FileNotFoundException - if the file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading.
      IOException - if an I/O error occurs.
      ???????:
      1.2
    • listFiles

      public static Collection<File> listFiles(File directory, IOFileFilter fileFilter, IOFileFilter dirFilter)
      Finds files within a given directory (and optionally its subdirectories). All files found are filtered by an IOFileFilter.

      If your search should recurse into subdirectories you can pass in an IOFileFilter for directories. You don't need to bind a DirectoryFileFilter (via logical AND) to this filter. This method does that for you.

      An example: If you want to search through all directories called "temp" you pass in FileFilterUtils.NameFileFilter("temp")

      Another common usage of this method is find files in a directory tree but ignoring the directories generated CVS. You can simply pass in FileFilterUtils.makeCVSAware(null).

      ??:
      directory - the directory to search in
      fileFilter - filter to apply when finding files. Must not be null, use TrueFileFilter.INSTANCE to match all files in selected directories.
      dirFilter - optional filter to apply when finding subdirectories. If this parameter is null, subdirectories will not be included in the search. Use TrueFileFilter.INSTANCE to match all directories.
      ??:
      a collection of java.io.File with the matching files
      ????:
    • listFiles

      public static Collection<File> listFiles(File directory, String[] extensions, boolean recursive)
      Finds files within a given directory (and optionally its subdirectories) which match an array of extensions.
      ??:
      directory - the directory to search in
      extensions - an array of extensions, ex. {"java","xml"}. If this parameter is null, all files are returned.
      recursive - if true all subdirectories are searched as well
      ??:
      a collection of java.io.File with the matching files
    • listFilesAndDirs

      public static Collection<File> listFilesAndDirs(File directory, IOFileFilter fileFilter, IOFileFilter dirFilter)
      Finds files within a given directory (and optionally its subdirectories). All files found are filtered by an IOFileFilter.

      The resulting collection includes the starting directory and any subdirectories that match the directory filter.

      ??:
      directory - the directory to search in
      fileFilter - filter to apply when finding files.
      dirFilter - optional filter to apply when finding subdirectories. If this parameter is null, subdirectories will not be included in the search. Use TrueFileFilter.INSTANCE to match all directories.
      ??:
      a collection of java.io.File with the matching files
      ???????:
      2.2
      ????:
    • moveDirectory

      public static void moveDirectory(File srcDir, File destDir) throws IOException
      Moves a directory.

      When the destination directory is on another file system, do a "copy and delete".

      ??:
      srcDir - the directory to be moved.
      destDir - the destination directory.
      ??:
      NullPointerException - if any of the given Files are null.
      IllegalArgumentException - if the source or destination is invalid.
      FileNotFoundException - if the source does not exist.
      IOException - if an error occurs or setting the last-modified time didn't succeeded.
      ???????:
      1.4
    • moveDirectoryToDirectory

      public static void moveDirectoryToDirectory(File src, File destDir, boolean createDestDir) throws IOException
      Moves a directory to another directory.
      ??:
      src - the file to be moved.
      destDir - the destination file.
      createDestDir - If true create the destination directory, otherwise if false throw an IOException.
      ??:
      NullPointerException - if any of the given Files are null.
      IllegalArgumentException - if the source or destination is invalid.
      FileNotFoundException - if the source does not exist.
      IOException - if an error occurs or setting the last-modified time didn't succeeded.
      ???????:
      1.4
    • moveFile

      public static void moveFile(File srcFile, File destFile) throws IOException
      Moves a file preserving attributes.

      Shorthand for moveFile(srcFile, destFile, StandardCopyOption.COPY_ATTRIBUTES).

      When the destination file is on another file system, do a "copy and delete".

      ??:
      srcFile - the file to be moved.
      destFile - the destination file.
      ??:
      NullPointerException - if any of the given Files are null.
      FileExistsException - if the destination file exists.
      IOException - if source or destination is invalid.
      IOException - if an error occurs.
      ???????:
      1.4
    • moveFile

      public static void moveFile(File srcFile, File destFile, CopyOption... copyOptions) throws IOException
      Moves a file.

      When the destination file is on another file system, do a "copy and delete".

      ??:
      srcFile - the file to be moved.
      destFile - the destination file.
      copyOptions - Copy options.
      ??:
      NullPointerException - if any of the given Files are null.
      FileExistsException - if the destination file exists.
      IOException - if source or destination is invalid.
      IOException - if an error occurs or setting the last-modified time didn't succeeded.
      ???????:
      2.9.0
    • moveFileToDirectory

      public static void moveFileToDirectory(File srcFile, File destDir, boolean createDestDir) throws IOException
      Moves a file to a directory.
      ??:
      srcFile - the file to be moved.
      destDir - the destination file.
      createDestDir - If true create the destination directory, otherwise if false throw an IOException.
      ??:
      NullPointerException - if any of the given Files are null.
      FileExistsException - if the destination file exists.
      IOException - if source or destination is invalid.
      IOException - if an error occurs or setting the last-modified time didn't succeeded.
      ???????:
      1.4
    • moveToDirectory

      public static void moveToDirectory(File src, File destDir, boolean createDestDir) throws IOException
      Moves a file or directory to the destination directory.

      When the destination is on another file system, do a "copy and delete".

      ??:
      src - the file or directory to be moved.
      destDir - the destination directory.
      createDestDir - If true create the destination directory, otherwise if false throw an IOException.
      ??:
      NullPointerException - if any of the given Files are null.
      FileExistsException - if the directory or file exists in the destination directory.
      IOException - if source or destination is invalid.
      IOException - if an error occurs or setting the last-modified time didn't succeeded.
      ???????:
      1.4
    • openInputStream

      public static FileInputStream openInputStream(File file) throws IOException
      Opens a FileInputStream for the specified file, providing better error messages than simply calling new FileInputStream(file).

      At the end of the method either the stream will be successfully opened, or an exception will have been thrown.

      An exception is thrown if the file does not exist. An exception is thrown if the file object exists but is a directory. An exception is thrown if the file exists but cannot be read.

      ??:
      file - the file to open for input, must not be null
      ??:
      a new FileInputStream for the specified file
      ??:
      NullPointerException - if file is null.
      FileNotFoundException - if the file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading.
      IOException - See FileNotFoundException above, FileNotFoundException is a subclass of IOException.
      ???????:
      1.3
    • openOutputStream

      public static FileOutputStream openOutputStream(File file) throws IOException
      Opens a FileOutputStream for the specified file, checking and creating the parent directory if it does not exist.

      At the end of the method either the stream will be successfully opened, or an exception will have been thrown.

      The parent directory will be created if it does not exist. The file will be created if it does not exist. An exception is thrown if the file object exists but is a directory. An exception is thrown if the file exists but cannot be written to. An exception is thrown if the parent directory cannot be created.

      ??:
      file - the file to open for output, must not be null
      ??:
      a new FileOutputStream for the specified file
      ??:
      NullPointerException - if the file object is null.
      IllegalArgumentException - if the file object is a directory
      IllegalArgumentException - if the file is not writable.
      IOException - if the directories could not be created.
      ???????:
      1.3
    • openOutputStream

      public static FileOutputStream openOutputStream(File file, boolean append) throws IOException
      Opens a FileOutputStream for the specified file, checking and creating the parent directory if it does not exist.

      At the end of the method either the stream will be successfully opened, or an exception will have been thrown.

      The parent directory will be created if it does not exist. The file will be created if it does not exist. An exception is thrown if the file object exists but is a directory. An exception is thrown if the file exists but cannot be written to. An exception is thrown if the parent directory cannot be created.

      ??:
      file - the file to open for output, must not be null
      append - if true, then bytes will be added to the end of the file rather than overwriting
      ??:
      a new FileOutputStream for the specified file
      ??:
      NullPointerException - if the file object is null.
      IllegalArgumentException - if the file object is a directory
      IllegalArgumentException - if the file is not writable.
      IOException - if the directories could not be created.
      ???????:
      2.1
    • readFileToByteArray

      public static byte[] readFileToByteArray(File file) throws IOException
      Reads the contents of a file into a byte array. The file is always closed.
      ??:
      file - the file to read, must not be null
      ??:
      the file contents, never null
      ??:
      NullPointerException - if file is null.
      FileNotFoundException - if the file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading.
      IOException - if an I/O error occurs.
      ???????:
      1.1
    • readFileToString

      @Deprecated public static String readFileToString(File file) throws IOException
      ????
      2.5 use readFileToString(File, Charset) instead (and specify the appropriate encoding)
      Reads the contents of a file into a String using the default encoding for the VM. The file is always closed.
      ??:
      file - the file to read, must not be null
      ??:
      the file contents, never null
      ??:
      NullPointerException - if file is null.
      FileNotFoundException - if the file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading.
      IOException - if an I/O error occurs.
      ???????:
      1.3.1
    • readFileToString

      public static String readFileToString(File file, Charset charsetName) throws IOException
      Reads the contents of a file into a String. The file is always closed.
      ??:
      file - the file to read, must not be null
      charsetName - the name of the requested charset, null means platform default
      ??:
      the file contents, never null
      ??:
      NullPointerException - if file is null.
      FileNotFoundException - if the file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading.
      IOException - if an I/O error occurs.
      ???????:
      2.3
    • readFileToString

      public static String readFileToString(File file, String charsetName) throws IOException
      Reads the contents of a file into a String. The file is always closed.
      ??:
      file - the file to read, must not be null
      charsetName - the name of the requested charset, null means platform default
      ??:
      the file contents, never null
      ??:
      NullPointerException - if file is null.
      FileNotFoundException - if the file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading.
      IOException - if an I/O error occurs.
      UnsupportedCharsetException - thrown instead of .UnsupportedEncodingException in version 2.2 if the named charset is unavailable.
      ???????:
      2.3
    • readLines

      @Deprecated public static List<String> readLines(File file) throws IOException
      ????
      2.5 use readLines(File, Charset) instead (and specify the appropriate encoding)
      Reads the contents of a file line by line to a List of Strings using the default encoding for the VM. The file is always closed.
      ??:
      file - the file to read, must not be null
      ??:
      the list of Strings representing each line in the file, never null
      ??:
      NullPointerException - if file is null.
      FileNotFoundException - if the file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading.
      IOException - if an I/O error occurs.
      ???????:
      1.3
    • readLines

      public static List<String> readLines(File file, Charset charset) throws IOException
      Reads the contents of a file line by line to a List of Strings. The file is always closed.
      ??:
      file - the file to read, must not be null
      charset - the charset to use, null means platform default
      ??:
      the list of Strings representing each line in the file, never null
      ??:
      NullPointerException - if file is null.
      FileNotFoundException - if the file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading.
      IOException - if an I/O error occurs.
      ???????:
      2.3
    • readLines

      public static List<String> readLines(File file, String charsetName) throws IOException
      Reads the contents of a file line by line to a List of Strings. The file is always closed.
      ??:
      file - the file to read, must not be null
      charsetName - the name of the requested charset, null means platform default
      ??:
      the list of Strings representing each line in the file, never null
      ??:
      NullPointerException - if file is null.
      FileNotFoundException - if the file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading.
      IOException - if an I/O error occurs.
      UnsupportedCharsetException - thrown instead of .UnsupportedEncodingException in version 2.2 if the named charset is unavailable.
      ???????:
      1.1
    • sizeOf

      public static long sizeOf(File file)
      Returns the size of the specified file or directory. If the provided File is a regular file, then the file's length is returned. If the argument is a directory, then the size of the directory is calculated recursively. If a directory or subdirectory is security restricted, its size will not be included.

      Note that overflow is not detected, and the return value may be negative if overflow occurs. See sizeOfAsBigInteger(File) for an alternative method that does not overflow.

      ??:
      file - the regular file or directory to return the size of (must not be null).
      ??:
      the length of the file, or recursive size of the directory, provided (in bytes).
      ??:
      NullPointerException - if the file is null.
      IllegalArgumentException - if the file does not exist.
      ???????:
      2.0
    • sizeOfAsBigInteger

      public static BigInteger sizeOfAsBigInteger(File file)
      Returns the size of the specified file or directory. If the provided File is a regular file, then the file's length is returned. If the argument is a directory, then the size of the directory is calculated recursively. If a directory or subdirectory is security restricted, its size will not be included.
      ??:
      file - the regular file or directory to return the size of (must not be null).
      ??:
      the length of the file, or recursive size of the directory, provided (in bytes).
      ??:
      NullPointerException - if the file is null.
      IllegalArgumentException - if the file does not exist.
      ???????:
      2.4
    • sizeOfDirectory

      public static long sizeOfDirectory(File directory)
      Counts the size of a directory recursively (sum of the length of all files).

      Note that overflow is not detected, and the return value may be negative if overflow occurs. See sizeOfDirectoryAsBigInteger(File) for an alternative method that does not overflow.

      ??:
      directory - directory to inspect, must not be null.
      ??:
      size of directory in bytes, 0 if directory is security restricted, a negative number when the real total is greater than Long.MAX_VALUE.
      ??:
      NullPointerException - if the directory is null.
    • sizeOfDirectoryAsBigInteger

      public static BigInteger sizeOfDirectoryAsBigInteger(File directory)
      Counts the size of a directory recursively (sum of the length of all files).
      ??:
      directory - directory to inspect, must not be null.
      ??:
      size of directory in bytes, 0 if directory is security restricted.
      ??:
      NullPointerException - if the directory is null.
      ???????:
      2.4
    • streamFiles

      public static Stream<File> streamFiles(File directory, boolean recursive, String... extensions) throws IOException
      Streams over the files in a given directory (and optionally its subdirectories) which match an array of extensions.
      ??:
      directory - the directory to search in
      recursive - if true all subdirectories are searched as well
      extensions - an array of extensions, ex. {"java","xml"}. If this parameter is null, all files are returned.
      ??:
      an iterator of java.io.File with the matching files
      ??:
      IOException - if an I/O error is thrown when accessing the starting file.
      ???????:
      2.9.0
    • toFile

      public static File toFile(URL url)
      Converts from a URL to a File.

      From version 1.1 this method will decode the URL. Syntax such as file:///my%20docs/file.txt will be correctly decoded to /my docs/file.txt. Starting with version 1.5, this method uses UTF-8 to decode percent-encoded octets to characters. Additionally, malformed percent-encoded octets are handled leniently by passing them through literally.

      ??:
      url - the file URL to convert, null returns null
      ??:
      the equivalent File object, or null if the URL's protocol is not file
    • toFiles

      public static File[] toFiles(URL... urls)
      Converts each of an array of URL to a File.

      Returns an array of the same size as the input. If the input is null, an empty array is returned. If the input contains null, the output array contains null at the same index.

      This method will decode the URL. Syntax such as file:///my%20docs/file.txt will be correctly decoded to /my docs/file.txt.

      ??:
      urls - the file URLs to convert, null returns empty array
      ??:
      a non-null array of Files matching the input, with a null item if there was a null at that index in the input array
      ??:
      IllegalArgumentException - if any file is not a URL file
      IllegalArgumentException - if any file is incorrectly encoded
      ???????:
      1.1
    • touch

      public static void touch(File file) throws IOException
      Implements the same behavior as the "touch" utility on Unix. It creates a new file with size 0 or, if the file exists already, it is opened and closed without modifying it, but updating the file date and time.

      NOTE: As from v1.3, this method throws an IOException if the last modified date of the file cannot be set. Also, as from v1.3 this method creates parent directories if they do not exist.

      ??:
      file - the File to touch.
      ??:
      IOException - if an I/O problem occurs.
      IOException - if setting the last-modified time failed.
    • toURLs

      public static URL[] toURLs(File... files) throws IOException
      Converts each of an array of File to a URL.

      Returns an array of the same size as the input.

      ??:
      files - the files to convert, must not be null
      ??:
      an array of URLs matching the input
      ??:
      IOException - if a file cannot be converted
      NullPointerException - if the parameter is null
    • waitFor

      public static boolean waitFor(File file, int seconds)
      Waits for NFS to propagate a file creation, imposing a timeout.

      This method repeatedly tests File.exists() until it returns true up to the maximum time specified in seconds.

      ??:
      file - the file to check, must not be null
      seconds - the maximum time in seconds to wait
      ??:
      true if file exists
      ??:
      NullPointerException - if the file is null
    • write

      @Deprecated public static void write(File file, CharSequence data) throws IOException
      ????
      2.5 use write(File, CharSequence, Charset) instead (and specify the appropriate encoding)
      Writes a CharSequence to a file creating the file if it does not exist using the default encoding for the VM.
      ??:
      file - the file to write
      data - the content to write to the file
      ??:
      IOException - in case of an I/O error
      ???????:
      2.0
    • write

      @Deprecated public static void write(File file, CharSequence data, boolean append) throws IOException
      ????
      2.5 use write(File, CharSequence, Charset, boolean) instead (and specify the appropriate encoding)
      Writes a CharSequence to a file creating the file if it does not exist using the default encoding for the VM.
      ??:
      file - the file to write
      data - the content to write to the file
      append - if true, then the data will be added to the end of the file rather than overwriting
      ??:
      IOException - in case of an I/O error
      ???????:
      2.1
    • write

      public static void write(File file, CharSequence data, Charset charset) throws IOException
      Writes a CharSequence to a file creating the file if it does not exist.
      ??:
      file - the file to write
      data - the content to write to the file
      charset - the name of the requested charset, null means platform default
      ??:
      IOException - in case of an I/O error
      ???????:
      2.3
    • write

      public static void write(File file, CharSequence data, Charset charset, boolean append) throws IOException
      Writes a CharSequence to a file creating the file if it does not exist.
      ??:
      file - the file to write
      data - the content to write to the file
      charset - the charset to use, null means platform default
      append - if true, then the data will be added to the end of the file rather than overwriting
      ??:
      IOException - in case of an I/O error
      ???????:
      2.3
    • write

      public static void write(File file, CharSequence data, String charsetName) throws IOException
      Writes a CharSequence to a file creating the file if it does not exist.
      ??:
      file - the file to write
      data - the content to write to the file
      charsetName - the name of the requested charset, null means platform default
      ??:
      IOException - in case of an I/O error
      UnsupportedEncodingException - if the encoding is not supported by the VM
      ???????:
      2.0
    • write

      public static void write(File file, CharSequence data, String charsetName, boolean append) throws IOException
      Writes a CharSequence to a file creating the file if it does not exist.
      ??:
      file - the file to write
      data - the content to write to the file
      charsetName - the name of the requested charset, null means platform default
      append - if true, then the data will be added to the end of the file rather than overwriting
      ??:
      IOException - in case of an I/O error
      UnsupportedCharsetException - thrown instead of .UnsupportedEncodingException in version 2.2 if the encoding is not supported by the VM
      ???????:
      2.1
    • writeByteArrayToFile

      public static void writeByteArrayToFile(File file, byte[] data) throws IOException
      Writes a byte array to a file creating the file if it does not exist.

      NOTE: As from v1.3, the parent directories of the file will be created if they do not exist.

      ??:
      file - the file to write to
      data - the content to write to the file
      ??:
      IOException - in case of an I/O error
      ???????:
      1.1
    • writeByteArrayToFile

      public static void writeByteArrayToFile(File file, byte[] data, boolean append) throws IOException
      Writes a byte array to a file creating the file if it does not exist.
      ??:
      file - the file to write to
      data - the content to write to the file
      append - if true, then bytes will be added to the end of the file rather than overwriting
      ??:
      IOException - in case of an I/O error
      ???????:
      2.1
    • writeByteArrayToFile

      public static void writeByteArrayToFile(File file, byte[] data, int off, int len) throws IOException
      Writes len bytes from the specified byte array starting at offset off to a file, creating the file if it does not exist.
      ??:
      file - the file to write to
      data - the content to write to the file
      off - the start offset in the data
      len - the number of bytes to write
      ??:
      IOException - in case of an I/O error
      ???????:
      2.5
    • writeByteArrayToFile

      public static void writeByteArrayToFile(File file, byte[] data, int off, int len, boolean append) throws IOException
      Writes len bytes from the specified byte array starting at offset off to a file, creating the file if it does not exist.
      ??:
      file - the file to write to
      data - the content to write to the file
      off - the start offset in the data
      len - the number of bytes to write
      append - if true, then bytes will be added to the end of the file rather than overwriting
      ??:
      IOException - in case of an I/O error
      ???????:
      2.5
    • writeLines

      public static void writeLines(File file, Collection<?> lines) throws IOException
      Writes the toString() value of each item in a collection to the specified File line by line. The default VM encoding and the default line ending will be used.
      ??:
      file - the file to write to
      lines - the lines to write, null entries produce blank lines
      ??:
      IOException - in case of an I/O error
      ???????:
      1.3
    • writeLines

      public static void writeLines(File file, Collection<?> lines, boolean append) throws IOException
      Writes the toString() value of each item in a collection to the specified File line by line. The default VM encoding and the default line ending will be used.
      ??:
      file - the file to write to
      lines - the lines to write, null entries produce blank lines
      append - if true, then the lines will be added to the end of the file rather than overwriting
      ??:
      IOException - in case of an I/O error
      ???????:
      2.1
    • writeLines

      public static void writeLines(File file, Collection<?> lines, String lineEnding) throws IOException
      Writes the toString() value of each item in a collection to the specified File line by line. The default VM encoding and the specified line ending will be used.
      ??:
      file - the file to write to
      lines - the lines to write, null entries produce blank lines
      lineEnding - the line separator to use, null is system default
      ??:
      IOException - in case of an I/O error
      ???????:
      1.3
    • writeLines

      public static void writeLines(File file, Collection<?> lines, String lineEnding, boolean append) throws IOException
      Writes the toString() value of each item in a collection to the specified File line by line. The default VM encoding and the specified line ending will be used.
      ??:
      file - the file to write to
      lines - the lines to write, null entries produce blank lines
      lineEnding - the line separator to use, null is system default
      append - if true, then the lines will be added to the end of the file rather than overwriting
      ??:
      IOException - in case of an I/O error
      ???????:
      2.1
    • writeLines

      public static void writeLines(File file, String charsetName, Collection<?> lines) throws IOException
      Writes the toString() value of each item in a collection to the specified File line by line. The specified character encoding and the default line ending will be used.

      NOTE: As from v1.3, the parent directories of the file will be created if they do not exist.

      ??:
      file - the file to write to
      charsetName - the name of the requested charset, null means platform default
      lines - the lines to write, null entries produce blank lines
      ??:
      IOException - in case of an I/O error
      UnsupportedEncodingException - if the encoding is not supported by the VM
      ???????:
      1.1
    • writeLines

      public static void writeLines(File file, String charsetName, Collection<?> lines, boolean append) throws IOException
      Writes the toString() value of each item in a collection to the specified File line by line, optionally appending. The specified character encoding and the default line ending will be used.
      ??:
      file - the file to write to
      charsetName - the name of the requested charset, null means platform default
      lines - the lines to write, null entries produce blank lines
      append - if true, then the lines will be added to the end of the file rather than overwriting
      ??:
      IOException - in case of an I/O error
      UnsupportedEncodingException - if the encoding is not supported by the VM
      ???????:
      2.1
    • writeLines

      public static void writeLines(File file, String charsetName, Collection<?> lines, String lineEnding) throws IOException
      Writes the toString() value of each item in a collection to the specified File line by line. The specified character encoding and the line ending will be used.

      NOTE: As from v1.3, the parent directories of the file will be created if they do not exist.

      ??:
      file - the file to write to
      charsetName - the name of the requested charset, null means platform default
      lines - the lines to write, null entries produce blank lines
      lineEnding - the line separator to use, null is system default
      ??:
      IOException - in case of an I/O error
      UnsupportedEncodingException - if the encoding is not supported by the VM
      ???????:
      1.1
    • writeLines

      public static void writeLines(File file, String charsetName, Collection<?> lines, String lineEnding, boolean append) throws IOException
      Writes the toString() value of each item in a collection to the specified File line by line. The specified character encoding and the line ending will be used.
      ??:
      file - the file to write to
      charsetName - the name of the requested charset, null means platform default
      lines - the lines to write, null entries produce blank lines
      lineEnding - the line separator to use, null is system default
      append - if true, then the lines will be added to the end of the file rather than overwriting
      ??:
      IOException - in case of an I/O error
      UnsupportedEncodingException - if the encoding is not supported by the VM
      ???????:
      2.1
    • writeStringToFile

      @Deprecated public static void writeStringToFile(File file, String data) throws IOException
      ????
      2.5 use writeStringToFile(File, String, Charset) instead (and specify the appropriate encoding)
      Writes a String to a file creating the file if it does not exist using the default encoding for the VM.
      ??:
      file - the file to write
      data - the content to write to the file
      ??:
      IOException - in case of an I/O error
    • writeStringToFile

      @Deprecated public static void writeStringToFile(File file, String data, boolean append) throws IOException
      ????
      2.5 use writeStringToFile(File, String, Charset, boolean) instead (and specify the appropriate encoding)
      Writes a String to a file creating the file if it does not exist using the default encoding for the VM.
      ??:
      file - the file to write
      data - the content to write to the file
      append - if true, then the String will be added to the end of the file rather than overwriting
      ??:
      IOException - in case of an I/O error
      ???????:
      2.1
    • writeStringToFile

      public static void writeStringToFile(File file, String data, Charset charset) throws IOException
      Writes a String to a file creating the file if it does not exist.

      NOTE: As from v1.3, the parent directories of the file will be created if they do not exist.

      ??:
      file - the file to write
      data - the content to write to the file
      charset - the charset to use, null means platform default
      ??:
      IOException - in case of an I/O error
      UnsupportedEncodingException - if the encoding is not supported by the VM
      ???????:
      2.4
    • writeStringToFile

      public static void writeStringToFile(File file, String data, Charset charset, boolean append) throws IOException
      Writes a String to a file creating the file if it does not exist.
      ??:
      file - the file to write
      data - the content to write to the file
      charset - the charset to use, null means platform default
      append - if true, then the String will be added to the end of the file rather than overwriting
      ??:
      IOException - in case of an I/O error
      ???????:
      2.3
    • writeStringToFile

      public static void writeStringToFile(File file, String data, String charsetName) throws IOException
      Writes a String to a file creating the file if it does not exist.

      NOTE: As from v1.3, the parent directories of the file will be created if they do not exist.

      ??:
      file - the file to write
      data - the content to write to the file
      charsetName - the name of the requested charset, null means platform default
      ??:
      IOException - in case of an I/O error
      UnsupportedEncodingException - if the encoding is not supported by the VM
    • writeStringToFile

      public static void writeStringToFile(File file, String data, String charsetName, boolean append) throws IOException
      Writes a String to a file creating the file if it does not exist.
      ??:
      file - the file to write
      data - the content to write to the file
      charsetName - the name of the requested charset, null means platform default
      append - if true, then the String will be added to the end of the file rather than overwriting
      ??:
      IOException - in case of an I/O error
      UnsupportedCharsetException - thrown instead of .UnsupportedEncodingException in version 2.2 if the encoding is not supported by the VM
      ???????:
      2.1