Class FileUtils

    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.io.File[] EMPTY_FILE_ARRAY
      An empty immutable File array.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static int cleanDirectory​(java.io.File directory)
      Cleans a directory without deleting it.
      static int deleteDirectory​(java.io.File directory)
      Deletes a directory recursively.
      static void deleteDirectoryOnExit​(java.io.File directory)
      Schedules a directory recursively for deletion on JVM exit.
      static int forceDelete​(java.io.File file)
      Deletes a file.
      static void forceDeleteOnExit​(java.io.File file)
      Schedules a file to be deleted when JVM exits.
      static java.io.File getCanonicalFile​(java.io.File file)
      Invoke File.getCanonicalFile() without throwing IOException.
      static java.lang.String getFileExtension​(java.lang.String fileName)
      Get File Extension
      static boolean isSymlink​(java.io.File file)  
      static java.io.File[] listFiles​(java.io.File directory)
      List Files from the specified directory
      static java.lang.String resolveRelativePath​(java.io.File parentDirectory, java.io.File targetFile)
      Resolve Relative Path
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • EMPTY_FILE_ARRAY

        public static final java.io.File[] EMPTY_FILE_ARRAY
        An empty immutable File array.
    • Method Detail

      • resolveRelativePath

        public static java.lang.String resolveRelativePath​(java.io.File parentDirectory,
                                                           java.io.File targetFile)
        Resolve Relative Path
        Parameters:
        parentDirectory - Parent Directory
        targetFile - Target File
        Returns:
        If targetFile is a sub-file of parentDirectory , resolve relative path, or null
      • getFileExtension

        public static java.lang.String getFileExtension​(java.lang.String fileName)
        Get File Extension
        Parameters:
        fileName - the name of File
        Returns:
        the file extension if found
      • deleteDirectory

        public static int deleteDirectory​(java.io.File directory)
                                   throws java.io.IOException
        Deletes a directory recursively.
        Parameters:
        directory - directory to delete
        Returns:
        the number of deleted files and directories
        Throws:
        java.lang.NullPointerException - if the directory is null
        java.io.IOException - in case deletion is unsuccessful
      • cleanDirectory

        public static int cleanDirectory​(java.io.File directory)
                                  throws java.io.IOException
        Cleans a directory without deleting it.
        Parameters:
        directory - directory to clean
        Throws:
        java.lang.NullPointerException - if the directory is null
        java.io.IOException - in case cleaning is unsuccessful
      • forceDelete

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

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

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

        public static void forceDeleteOnExit​(java.io.File file)
        Schedules a file to be deleted when JVM exits. If file is directory delete it and all sub-directories.
        Parameters:
        file - file or directory to delete, must not be null
        Throws:
        java.lang.NullPointerException - if the file is null
      • deleteDirectoryOnExit

        public static void deleteDirectoryOnExit​(java.io.File directory)
        Schedules a directory recursively for deletion on JVM exit.
        Parameters:
        directory - directory to delete, must not be null
        Throws:
        java.lang.NullPointerException - if the directory is null
        java.io.IOException - in case deletion is unsuccessful
      • listFiles

        public static java.io.File[] listFiles​(java.io.File directory)
        List Files from the specified directory
        Parameters:
        directory - the specified directory
        Returns:
        the empty file array if the specified directory is not exist or not a directory
      • isSymlink

        public static boolean isSymlink​(java.io.File file)
      • getCanonicalFile

        public static final java.io.File getCanonicalFile​(java.io.File file)
        Invoke File.getCanonicalFile() without throwing IOException.
        Parameters:
        file - the File instance
        Returns:
        File.getCanonicalFile()
        Throws:
        java.lang.NullPointerException - if file is null
        java.lang.RuntimeException - If an I/O error occurs, which is possible because the construction of the canonical pathname may require filesystem queries