Class PathUtils


  • public final class PathUtils
    extends Object
    General path manipulation utilities.
    • Method Detail

      • copyDirectory

        public static void copyDirectory​(Path srcDir,
                                         Path 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, no indication is provided.

        Parameters:
        srcDir - an existing directory to copy, must not be null
        destDir - the new directory, must not be null
        Throws:
        NullPointerException - if source or destination is null
        IOException - if source or destination is invalid
        IOException - if an IO error occurs during copying
      • copyDirectory

        public static void copyDirectory​(Path srcDir,
                                         Path 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, no indication is provided.

        Parameters:
        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
        Throws:
        NullPointerException - if source or destination is null
        IOException - if source or destination is invalid
        IOException - if an IO error occurs during copying
      • copyDirectory

        public static void copyDirectory​(Path srcDir,
                                         Path destDir,
                                         Predicate<Path> 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, no indication is provided.

        Example: Copy directories only

          // only copy the directory structure
          FileUtils.copyDirectory(srcDir, destDir, Files::isDirectory);
          
        Parameters:
        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
        Throws:
        NullPointerException - if source or destination is null
        IOException - if source or destination is invalid
        IOException - if an IO error occurs during copying
      • copyDirectory

        public static void copyDirectory​(Path srcDir,
                                         Path destDir,
                                         Predicate<Path> 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, no indication is provided.

        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);
          
        Parameters:
        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
        Throws:
        NullPointerException - if source or destination is null
        IOException - if source or destination is invalid
        IOException - if an IO error occurs during copying
      • doCopyDirectory

        private static void doCopyDirectory​(Path srcDir,
                                            Path destDir,
                                            Predicate<Path> filter,
                                            boolean preserveFileDate,
                                            List<Path> exclusionList)
                                     throws IOException
        Internal copy directory method.
        Parameters:
        srcDir - the validated source directory, must not be null
        destDir - the validated destination directory, must not be null
        filter - the filter to apply, null means copy all directories and files
        preserveFileDate - whether to preserve the file date
        exclusionList - List of files and directories to exclude from the copy, may be null
        Throws:
        IOException - if an error occurs
      • doCopyFile

        private static void doCopyFile​(Path srcFile,
                                       Path destFile,
                                       boolean preserveFileDate)
                                throws IOException
        Internal copy file method. This caches the original file length, and throws an IOException if the output file length is different from the current input file length. So it may fail if the file changes size. It may also fail with "IllegalArgumentException: Negative size" if the input file is truncated part way through copying the data and the new file size is less than the current position.
        Parameters:
        srcFile - the validated source file, must not be null
        destFile - the validated destination file, must not be null
        preserveFileDate - whether to preserve the file date
        Throws:
        IOException - if an error occurs
        IOException - if the output file length is not the same as the input file length after the copy completes
        IllegalArgumentException - "Negative size" if the file is truncated so that the size is less than the position
      • moveFile

        public static void moveFile​(Path srcFile,
                                    Path destFile)
                             throws IOException
        Moves a file.

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

        Parameters:
        srcFile - the file to be moved
        destFile - the destination file
        Throws:
        NullPointerException - if source or destination is null
        FileExistsException - if the destination file exists
        IOException - if source or destination is invalid
        IOException - if an IO error occurs moving the file
        Since:
        1.4
      • moveToDirectory

        public static void moveToDirectory​(Path src,
                                           Path 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".

        Parameters:
        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
        Throws:
        NullPointerException - if source or destination is null
        FileExistsException - if the directory or file exists in the destination directory
        IOException - if source or destination is invalid
        IOException - if an IO error occurs moving the file
      • moveFileToDirectory

        public static void moveFileToDirectory​(Path srcFile,
                                               Path destDir,
                                               boolean createDestDir)
                                        throws IOException
        Moves a file to a directory.
        Parameters:
        srcFile - the file to be moved
        destDir - the destination file
        createDestDir - If true create the destination directory, otherwise if false throw an IOException
        Throws:
        NullPointerException - if source or destination is null
        FileExistsException - if the destination file exists
        IOException - if source or destination is invalid
        IOException - if an IO error occurs moving the file
        Since:
        1.4
      • moveDirectoryToDirectory

        public static void moveDirectoryToDirectory​(Path src,
                                                    Path destDir,
                                                    boolean createDestDir)
                                             throws IOException
        Moves a directory to another directory.
        Parameters:
        src - the file to be moved
        destDir - the destination file
        createDestDir - If true create the destination directory, otherwise if false throw an IOException
        Throws:
        NullPointerException - if source or destination is null
        FileExistsException - if the directory exists in the destination directory
        IOException - if source or destination is invalid
        IOException - if an IO error occurs moving the file
      • moveDirectory

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

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

        Parameters:
        srcDir - the directory to be moved
        destDir - the destination directory
        Throws:
        NullPointerException - if source or destination is null
        FileExistsException - if the destination directory exists
        IOException - if source or destination is invalid
        IOException - if an IO error occurs moving the file
      • deleteQuietly

        public static boolean deleteQuietly​(Path 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.
        Parameters:
        file - file or directory to delete, can be null
        Returns:
        true if the file or directory was deleted, otherwise false
      • verifiedListFiles

        private static Path[] verifiedListFiles​(Path directory)
                                         throws IOException
        Lists files in a directory, asserting that the supplied directory satisfies exists and is a directory
        Parameters:
        directory - The directory to list
        Returns:
        The files in the directory, never null.
        Throws:
        IOException - if an I/O error occurs
      • forceDelete

        public static void forceDelete​(Path file)
                                throws 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
        Throws:
        NullPointerException - if the directory is null
        FileNotFoundException - if the file was not found
        IOException - in case deletion is unsuccessful
      • isSymlink

        public static boolean isSymlink​(Path file)
                                 throws IOException
        Determines whether the specified file is a Symbolic Link rather than an actual file.

        Will not return true if there is a Symbolic Link anywhere in the path, only if the specific file is.

        When using jdk1.7, this method delegates to boolean java.nio.file.Files.isSymbolicLink(Path path)

        For code that runs on Java 1.7 or later, use the following method instead:
        boolean java.nio.file.Files.isSymbolicLink(Path path)

        Parameters:
        file - the file to check
        Returns:
        true if the file is a Symbolic Link
        Throws:
        IOException - if an IO error occurs while checking the file
      • copyFile

        public static void copyFile​(Path srcFile,
                                    Path 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, no indication is provided.

        Parameters:
        srcFile - an existing file to copy, must not be null
        destFile - the new file, must not be null
        Throws:
        NullPointerException - if source or destination is null
        IOException - if source or destination is invalid
        IOException - if an IO error occurs during copying
        IOException - if the output file length is not the same as the input file length after the copy completes
        See Also:
        copyFile(Path, Path, boolean)
      • copyFile

        public static void copyFile​(Path srcFile,
                                    Path destFile,
                                    boolean preserveFileDate)
                             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, 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, no indication is provided.

        Parameters:
        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
        Throws:
        NullPointerException - if source or destination is null
        IOException - if source or destination is invalid
        IOException - if an IO error occurs during copying
        IOException - if the output file length is not the same as the input file length after the copy completes
        See Also:
        doCopyFile(Path, Path, boolean)