Class FileUtils


  • public final class FileUtils
    extends Object
    A utility class providing simple methods for dealing with files and directories.
    Since:
    4.0.0
    Author:
    Dmytro Nosan
    • Method Detail

      • delete

        public static boolean delete​(Path path)
                              throws IOException
        Deletes the specified Path. If the path refers to a directory, it will recursively delete its contents before deleting the directory itself.

        This method is safe to use even if the specified path does not exist.

        Parameters:
        path - the path to delete; if null, this method will return false
        Returns:
        true if the path existed and was deleted, or false if it did not exist
        Throws:
        IOException - if an I/O error occurs while deleting the path
      • copy

        public static void copy​(Path src,
                                Path dest,
                                CopyOption... options)
                         throws IOException
        Copies a file or directory to the target location. If the source path refers to a directory, it will be copied recursively, including its contents.
        Parameters:
        src - the source path
        dest - the target path
        options - the options specifying how the copy should be performed
        Throws:
        IOException - if an I/O error occurs during the copy
        NullPointerException - if any of the arguments are null
      • copy

        public static void copy​(Path src,
                                Path dest,
                                BiPredicate<? super Path,​? super BasicFileAttributes> filter,
                                CopyOption... options)
                         throws IOException
        Copies a file or directory to the target location with a filtering function. If the source path refers to a directory, it will be copied recursively, but only files and directories that pass the filter will be included.
        Parameters:
        src - the source path
        dest - the destination path
        filter - a predicate used to determine whether a file or directory should be copied
        options - the options specifying how the copy should be performed
        Throws:
        IOException - if an I/O error occurs during the copy
        NullPointerException - if the source path, destination path, or options array is null
      • checksum

        public static String checksum​(Path file,
                                      String algorithm)
                               throws NoSuchAlgorithmException,
                                      IOException
        Computes the checksum of the specified file using the given algorithm.

        This method reads the file's content and computes the hash using the provided algorithm (e.g., MD5, SHA-1, SHA-256). The result is returned as a lowercase hexadecimal string.

        Parameters:
        file - the file for which the checksum is computed
        algorithm - the name of the algorithm to use for the hash computation
        Returns:
        the computed checksum as a lowercase hexadecimal string
        Throws:
        IOException - if an I/O error occurs while reading the file
        NoSuchAlgorithmException - if the specified algorithm is not available
        NullPointerException - if the file or algorithm is null