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 by deleting all files and sub-directories without deleting the directory itself.
      static int deleteDirectory​(java.io.File directory)
      Deletes a directory and returns the number of deleted files and directories.
      static void deleteDirectoryOnExit​(java.io.File directory)
      Schedules a directory for deletion on JVM exit, including all its contents.
      static int forceDelete​(java.io.File file)
      Deletes a file or directory and all its contents recursively.
      static void forceDeleteOnExit​(java.io.File file)
      Schedules a file or directory for deletion on JVM exit.
      static java.io.File getCanonicalFile​(java.io.File file)
      Returns the canonical form of the specified File.
      static java.lang.String getFileExtension​(java.lang.String fileName)
      Gets the extension of a file name, if any.
      static boolean isSymlink​(java.io.File file)
      Determines if the provided File is a symbolic link.
      static java.io.File[] listFiles​(java.io.File directory)
      Lists the files in the specified directory.
      static java.lang.String resolveRelativePath​(java.io.File parentDirectory, java.io.File targetFile)
      Resolves the relative path from a parent directory to a target file.
      • Methods inherited from class java.lang.Object

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

      • EMPTY_FILE_ARRAY

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

      • resolveRelativePath

        @Nullable
        public static java.lang.String resolveRelativePath​(java.io.File parentDirectory,
                                                           java.io.File targetFile)
        Resolves the relative path from a parent directory to a target file.

        If the targetFile is not under the specified parentDirectory, this method returns null. If the paths are equal, an empty string is returned.

        Example Usage

        • resolveRelativePath(new File("/home/user"), new File("/home/user/docs/file.txt")) returns "docs/file.txt"
        • resolveRelativePath(new File("/home/user"), new File("/home/user/file.txt")) returns "file.txt"
        • resolveRelativePath(new File("/home/user"), new File("/tmp/file.txt")) returns null
        • resolveRelativePath(new File("/home/user"), new File("/home/user")) returns an empty string
        Parameters:
        parentDirectory - the base directory to calculate the relative path from
        targetFile - the target file or directory whose relative path is to be determined
        Returns:
        the relative path from the parent directory to the target file, using forward slashes (/), or null if the target file is not under the parent directory
      • getFileExtension

        @Nullable
        public static java.lang.String getFileExtension​(java.lang.String fileName)
        Gets the extension of a file name, if any.

        Example Usage

        • getFileExtension("file.txt") returns "txt"
        • getFileExtension("file.tar.gz") returns "gz"
        • getFileExtension(".hiddenfile") returns null (no extension)
        • getFileExtension("file") returns null (no extension)
        • getFileExtension("") returns null (blank string)
        • getFileExtension(null) returns null
        Parameters:
        fileName - the name of the file, may be null or blank
        Returns:
        the file's extension without the dot (.), or null if there's no extension or input is blank
      • deleteDirectory

        public static int deleteDirectory​(java.io.File directory)
                                   throws java.io.IOException
        Deletes a directory and returns the number of deleted files and directories.

        If the directory does not exist, it is considered already deleted, and this method returns 0.

        Example Usage

        • deleteDirectory(new File("/tmp/testDir")) deletes the directory and all its contents, returning the total count of deleted files and directories.
        • deleteDirectory(new File("/nonexistent/dir")) returns 0 since the directory does not exist.
        Parameters:
        directory - the directory to delete, must not be null
        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 by deleting all files and sub-directories without deleting the directory itself.

        This method recursively deletes all files and directories within the provided directory. If any file or sub-directory cannot be deleted, an IOException is thrown after attempting to delete as many as possible.

        1

        Example Usage

        
         File dir = new File("/path/to/directory");
         int deletedCount = cleanDirectory(dir);
         System.out.println("Deleted " + deletedCount + " files/directories.");
         
        Parameters:
        directory - the directory to clean, must not be null
        Returns:
        the number of deleted files and directories
        Throws:
        java.lang.NullPointerException - if the directory is null
        java.io.IOException - if deletion fails for any file or sub-directory
      • forceDelete

        public static int forceDelete​(java.io.File file)
                               throws java.io.IOException
        Deletes a file or directory and all its contents recursively.

        If the provided file is a directory, this method deletes all sub-directories and files, then deletes the directory itself. If it's a regular file, it deletes that single file.

        Example Usage

        • forceDelete(new File("/tmp/file.txt")) deletes the file and returns 1
        • forceDelete(new File("/tmp/testDir")) deletes the directory and all its contents, returning the total count of deleted files and directories.
        • forceDelete(new File("/nonexistent/file")) throws a FileNotFoundException
        Parameters:
        file - the 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 does not exist
        java.io.IOException - if deletion fails for any reason
      • forceDeleteOnExit

        public static void forceDeleteOnExit​(java.io.File file)
        Schedules a file or directory for deletion on JVM exit.

        If the provided file is a directory, this method schedules all sub-directories and files, then schedules the directory itself. If it's a regular file, it schedules that single file.

        Example Usage

        • forceDeleteOnExit(new File("/tmp/file.txt")) schedules the file for deletion on exit.
        • forceDeleteOnExit(new File("/tmp/testDir")) schedules the directory and all its contents for deletion on exit.
        Parameters:
        file - the file or directory to schedule for deletion, 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 for deletion on JVM exit, including all its contents.

        If the directory does not exist, this method does nothing. If it does exist, it schedules the directory for deletion and recursively schedules all files and subdirectories for deletion.

        Example Usage

        • deleteDirectoryOnExit(new File("/tmp/testDir")) ensures that the directory and all its contents are deleted when the JVM exits.
        • deleteDirectoryOnExit(new File("/nonexistent/dir")) does nothing since the directory does not exist.
        Parameters:
        directory - the directory to schedule for deletion on exit, must not be null
        Throws:
        java.lang.NullPointerException - if the directory is null
      • listFiles

        @Nonnull
        public static java.io.File[] listFiles​(java.io.File directory)
        Lists the files in the specified directory.

        If the provided directory is not valid (i.e., it does not exist, or it is not a directory), this method returns an empty file array.

        Example Usage

        • listFiles(new File("/tmp")) returns an array of files in the "/tmp" directory.
        • listFiles(new File("/nonexistent/dir")) returns an empty file array since the directory does not exist.
        • listFiles(null) returns an empty file array as the input is null.
        Parameters:
        directory - the directory to list files from
        Returns:
        an array of File objects representing the files in the specified directory, or EMPTY_FILE_ARRAY if the directory is not valid
      • isSymlink

        public static boolean isSymlink​(java.io.File file)
        Determines if the provided File is a symbolic link.

        Example Usage

        • isSymlink(new File("/path/to/symlink")) returns true if it's a symbolic link.
        • isSymlink(new File("/path/to/regularfile")) returns false as it's not a symbolic link.
        • isSymlink(null) throws a NullPointerException.
        Parameters:
        file - the file to check, must not be null
        Returns:
        true if the file is a symbolic link, otherwise false
        Throws:
        java.lang.NullPointerException - if the file is null
      • getCanonicalFile

        @Nonnull
        public static java.io.File getCanonicalFile​(java.io.File file)
        Returns the canonical form of the specified File.

        This method wraps the call to File.getCanonicalFile() in a try-catch block to handle any checked exceptions via the ThrowableSupplier utility.

        Example Usage

        • getCanonicalFile(new File("relative/path")) returns the canonical file object.
        • If the file does not exist or I/O error occurs, it will propagate as an unchecked exception.
        Parameters:
        file - the file for which the canonical representation is required, must not be null
        Returns:
        the canonical representation of the given file
        Throws:
        java.lang.NullPointerException - if the provided file is null
        java.lang.RuntimeException - if an I/O error occurs while retrieving the canonical file