Class FileUtils


  • public final class FileUtils
    extends Object
    This is a utility class to deal files and directories. Contains utilities for recursive deletion and creation of temporary files.
    • Method Detail

      • listDirectory

        public static Path[] listDirectory​(Path directory)
                                    throws IOException
        Lists the given directory in a resource-leak-safe way.
        Throws:
        IOException
      • getRandomFilename

        public static String getRandomFilename​(String prefix)
        Constructs a random filename with the given prefix and a random part generated from hex characters.
        Parameters:
        prefix - the prefix to the filename to be constructed
        Returns:
        the generated random filename with the given prefix
      • readAllBytes

        public static byte[] readAllBytes​(Path path)
                                   throws IOException
        Reads all the bytes from a file. The method ensures that the file is closed when all bytes have been read or an I/O error, or other runtime exception, is thrown.

        This is an implementation that follow Files.readAllBytes(java.nio.file.Path), and the difference is that it limits the size of the direct buffer to avoid direct-buffer OutOfMemoryError. When Files.readAllBytes(java.nio.file.Path) or other interfaces in java API can do this in the future, we should remove it.

        Parameters:
        path - the path to the file
        Returns:
        a byte array containing the bytes read from the file
        Throws:
        IOException - if an I/O error occurs reading from the stream
        OutOfMemoryError - if an array of the required size cannot be allocated, for example the file is larger that 2GB
      • deleteFileOrDirectory

        public static void deleteFileOrDirectory​(File file)
                                          throws IOException
        Removes the given file or directory recursively.

        If the file or directory does not exist, this does not throw an exception, but simply does nothing. It considers the fact that a file-to-be-deleted is not present a success.

        This method is safe against other concurrent deletion attempts.

        Parameters:
        file - The file or directory to delete.
        Throws:
        IOException - Thrown if the directory could not be cleaned for some reason, for example due to missing access/write permissions.
      • deleteDirectory

        public static void deleteDirectory​(File directory)
                                    throws IOException
        Deletes the given directory recursively.

        If the directory does not exist, this does not throw an exception, but simply does nothing. It considers the fact that a directory-to-be-deleted is not present a success.

        This method is safe against other concurrent deletion attempts.

        Parameters:
        directory - The directory to be deleted.
        Throws:
        IOException - Thrown if the given file is not a directory, or if the directory could not be deleted for some reason, for example due to missing access/write permissions.
      • deleteDirectoryQuietly

        public static void deleteDirectoryQuietly​(File directory)
        Deletes the given directory recursively, not reporting any I/O exceptions that occur.

        This method is identical to deleteDirectory(File), except that it swallows all exceptions and may leave the job quietly incomplete.

        Parameters:
        directory - The directory to delete.
      • cleanDirectory

        public static void cleanDirectory​(File directory)
                                   throws IOException
        Removes all files contained within a directory, without removing the directory itself.

        This method is safe against other concurrent deletion attempts.

        Parameters:
        directory - The directory to remove all files from.
        Throws:
        FileNotFoundException - Thrown if the directory itself does not exist.
        IOException - Thrown if the file indicates a proper file and not a directory, or if the directory could not be cleaned for some reason, for example due to missing access/write permissions.
      • copy

        public static void copy​(Path sourcePath,
                                Path targetPath,
                                boolean executable)
                         throws IOException
        Copies all files from source to target and sets executable flag. Paths might be on different systems.
        Parameters:
        sourcePath - source path to copy from
        targetPath - target path to copy to
        executable - if target file should be executable
        Throws:
        IOException - if the copy fails
      • expandDirectory

        public static Path expandDirectory​(Path file,
                                           Path targetDirectory)
                                    throws IOException
        Un-archives files inside the target directory. Illegal fs access outside target directory is not permitted.
        Parameters:
        file - path to zipped/archived file
        targetDirectory - directory path where file needs to be unarchived
        Returns:
        path to folder with unarchived contents
        Throws:
        IOException - if file open fails or in case of unsafe access outside target directory
      • listFilesInDirectory

        public static Collection<Path> listFilesInDirectory​(Path directory,
                                                            Predicate<Path> fileFilter)
                                                     throws IOException
        List the directory recursively and return the files that satisfy the fileFilter.
        Parameters:
        directory - the directory to be listed
        fileFilter - a file filter
        Returns:
        a collection of Files
        Throws:
        IOException - if an I/O error occurs while listing the files in the given directory
      • getDirectoryFilesSize

        public static long getDirectoryFilesSize​(Path path,
                                                 FileVisitOption... options)
                                          throws IOException
        Computes the sum of sizes of all files in the directory and it's subdirectories.
        Parameters:
        path - the root path from which to start the calculation.
        options - visitation options for the directory traversal.
        Returns:
        sum of sizes of all files in the directory and it's subdirectories.
        Throws:
        IOException - if the size cannot be determined.
      • absolutizePath

        public static Path absolutizePath​(Path pathToAbsolutize)
                                   throws IOException
        Absolutize the given path if it is relative.
        Parameters:
        pathToAbsolutize - path which is being absolutized if it is a relative path
        Returns:
        the absolutized path
        Throws:
        IOException
      • relativizePath

        public static Path relativizePath​(Path basePath,
                                          Path pathToRelativize)
        Relativize the given path with respect to the given base path if it is absolute.
        Parameters:
        basePath - to relativize against
        pathToRelativize - path which is being relativized if it is an absolute path
        Returns:
        the relativized path
      • getCurrentWorkingDirectory

        public static Path getCurrentWorkingDirectory()
        Returns the current working directory as specified by the user.dir system property.
        Returns:
        current working directory
      • isJarFile

        public static boolean isJarFile​(Path file)
        Checks whether the given file has a jar extension.
        Parameters:
        file - to check
        Returns:
        true if the file has a jar extension, otherwise false
      • stripFileExtension

        public static String stripFileExtension​(String fileName)
        Remove the extension of the file name.
        Parameters:
        fileName - to strip
        Returns:
        the file name without extension
      • getTargetPathIfContainsSymbolicPath

        public static Path getTargetPathIfContainsSymbolicPath​(Path path)
                                                        throws IOException
        Get a target path(the path that replaced symbolic links with linked path) if the original path contains symbolic path, return the original path otherwise.
        Parameters:
        path - the original path.
        Returns:
        the path that replaced symbolic links with real path.
        Throws:
        IOException
      • toURL

        public static URL toURL​(Path path)
                         throws MalformedURLException
        Converts the given Path into a file URL. The resulting url is relative iff the given path is relative.
        Parameters:
        path - to convert into a URL.
        Returns:
        URL
        Throws:
        MalformedURLException - if the path could not be converted into a file URL