org.apache.commons.io.file

Class PathUtils


  • public final class PathUtils
    extends Object
    NIO Path utilities.
    Since:
    2.7
    • Method Detail

      • cleanDirectory

        public static Counters.PathCounters cleanDirectory(Path directory)
                                                    throws IOException
        Cleans a directory including sub-directories without deleting directories.
        Parameters:
        directory - directory to clean.
        Returns:
        The visitation path counters.
        Throws:
        IOException - if an I/O error is thrown by a visitor method.
      • cleanDirectory

        public static Counters.PathCounters cleanDirectory(Path directory,
                                                           DeleteOption... deleteOptions)
                                                    throws IOException
        Cleans a directory including sub-directories without deleting directories.
        Parameters:
        directory - directory to clean.
        deleteOptions - How to handle deletion.
        Returns:
        The visitation path counters.
        Throws:
        IOException - if an I/O error is thrown by a visitor method.
        Since:
        2.8.0
      • copyDirectory

        public static Counters.PathCounters copyDirectory(Path sourceDirectory,
                                                          Path targetDirectory,
                                                          CopyOption... copyOptions)
                                                   throws IOException
        Copies a directory to another directory.
        Parameters:
        sourceDirectory - The source directory.
        targetDirectory - The target directory.
        copyOptions - Specifies how the copying should be done.
        Returns:
        The visitation path counters.
        Throws:
        IOException - if an I/O error is thrown by a visitor method.
      • copyFileToDirectory

        public static Path copyFileToDirectory(Path sourceFile,
                                               Path targetDirectory,
                                               CopyOption... copyOptions)
                                        throws IOException
        Copies a file to a directory.
        Parameters:
        sourceFile - The source file.
        targetDirectory - The target directory.
        copyOptions - Specifies how the copying should be done.
        Returns:
        The target file
        Throws:
        IOException - if an I/O error occurs.
        See Also:
        Files.copy(Path, Path, CopyOption...)
      • copyFileToDirectory

        public static Path copyFileToDirectory(URL sourceFile,
                                               Path targetDirectory,
                                               CopyOption... copyOptions)
                                        throws IOException
        Copies a URL to a directory.
        Parameters:
        sourceFile - The source URL.
        targetDirectory - The target directory.
        copyOptions - Specifies how the copying should be done.
        Returns:
        The target file
        Throws:
        IOException - if an I/O error occurs.
        See Also:
        Files.copy(InputStream, Path, CopyOption...)
      • countDirectory

        public static Counters.PathCounters countDirectory(Path directory)
                                                    throws IOException
        Counts aspects of a directory including sub-directories.
        Parameters:
        directory - directory to delete.
        Returns:
        The visitor used to count the given directory.
        Throws:
        IOException - if an I/O error is thrown by a visitor method.
      • createParentDirectories

        public static Path createParentDirectories(Path path,
                                                   FileAttribute<?>... attrs)
                                            throws IOException
        Creates the parent directories for the given path.
        Parameters:
        path - The path to a file (or directory).
        attrs - An optional list of file attributes to set atomically when creating the directories.
        Returns:
        The Path for the path's parent directory or null if the given path has no parent.
        Throws:
        IOException - if an I/O error occurs.
        Since:
        2.9.0
      • current

        public static Path current()
        Gets the current directory.
        Returns:
        the current directory.
        Since:
        2.9.0
      • delete

        public static Counters.PathCounters delete(Path path)
                                            throws IOException
        Deletes a file or directory. If the path is a directory, delete it and all sub-directories.

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

        • A directory to delete does not have to be empty.
        • You get exceptions when a file or directory cannot be deleted; File.delete() returns a boolean.
        Parameters:
        path - file or directory to delete, must not be null
        Returns:
        The visitor used to delete the given directory.
        Throws:
        NullPointerException - if the directory is null
        IOException - if an I/O error is thrown by a visitor method or if an I/O error occurs.
      • delete

        public static Counters.PathCounters delete(Path path,
                                                   DeleteOption... deleteOptions)
                                            throws IOException
        Deletes a file or directory. If the path is a directory, delete it and all sub-directories.

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

        • A directory to delete does not have to be empty.
        • You get exceptions when a file or directory cannot be deleted; File.delete() returns a boolean.
        Parameters:
        path - file or directory to delete, must not be null
        deleteOptions - How to handle deletion.
        Returns:
        The visitor used to delete the given directory.
        Throws:
        NullPointerException - if the directory is null
        IOException - if an I/O error is thrown by a visitor method or if an I/O error occurs.
        Since:
        2.8.0
      • delete

        public static Counters.PathCounters delete(Path path,
                                                   LinkOption[] linkOptions,
                                                   DeleteOption... deleteOptions)
                                            throws IOException
        Deletes a file or directory. If the path is a directory, delete it and all sub-directories.

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

        • A directory to delete does not have to be empty.
        • You get exceptions when a file or directory cannot be deleted; File.delete() returns a boolean.
        Parameters:
        path - file or directory to delete, must not be null
        linkOptions - How to handle symbolic links.
        deleteOptions - How to handle deletion.
        Returns:
        The visitor used to delete the given directory.
        Throws:
        NullPointerException - if the directory is null
        IOException - if an I/O error is thrown by a visitor method or if an I/O error occurs.
        Since:
        2.9.0
      • deleteDirectory

        public static Counters.PathCounters deleteDirectory(Path directory)
                                                     throws IOException
        Deletes a directory including sub-directories.
        Parameters:
        directory - directory to delete.
        Returns:
        The visitor used to delete the given directory.
        Throws:
        IOException - if an I/O error is thrown by a visitor method.
      • deleteDirectory

        public static Counters.PathCounters deleteDirectory(Path directory,
                                                            DeleteOption... deleteOptions)
                                                     throws IOException
        Deletes a directory including sub-directories.
        Parameters:
        directory - directory to delete.
        deleteOptions - How to handle deletion.
        Returns:
        The visitor used to delete the given directory.
        Throws:
        IOException - if an I/O error is thrown by a visitor method.
        Since:
        2.8.0
      • deleteDirectory

        public static Counters.PathCounters deleteDirectory(Path directory,
                                                            LinkOption[] linkOptions,
                                                            DeleteOption... deleteOptions)
                                                     throws IOException
        Deletes a directory including sub-directories.
        Parameters:
        directory - directory to delete.
        linkOptions - How to handle symbolic links.
        deleteOptions - How to handle deletion.
        Returns:
        The visitor used to delete the given directory.
        Throws:
        IOException - if an I/O error is thrown by a visitor method.
        Since:
        2.9.0
      • deleteFile

        public static Counters.PathCounters deleteFile(Path file)
                                                throws IOException
        Deletes the given file.
        Parameters:
        file - The file to delete.
        Returns:
        A visitor with path counts set to 1 file, 0 directories, and the size of the deleted file.
        Throws:
        IOException - if an I/O error occurs.
        NoSuchFileException - if the file is a directory.
      • deleteFile

        public static Counters.PathCounters deleteFile(Path file,
                                                       DeleteOption... deleteOptions)
                                                throws IOException
        Deletes the given file.
        Parameters:
        file - The file to delete.
        deleteOptions - How to handle deletion.
        Returns:
        A visitor with path counts set to 1 file, 0 directories, and the size of the deleted file.
        Throws:
        IOException - if an I/O error occurs.
        NoSuchFileException - if the file is a directory.
        Since:
        2.8.0
      • directoryAndFileContentEquals

        public static boolean directoryAndFileContentEquals(Path path1,
                                                            Path path2)
                                                     throws IOException
        Compares the file sets of two Paths to determine if they are equal or not while considering file contents. The comparison includes all files in all sub-directories.
        Parameters:
        path1 - The first directory.
        path2 - The second directory.
        Returns:
        Whether the two directories contain the same files while considering file contents.
        Throws:
        IOException - if an I/O error is thrown by a visitor method
      • directoryAndFileContentEquals

        public static boolean directoryAndFileContentEquals(Path path1,
                                                            Path path2,
                                                            LinkOption[] linkOptions,
                                                            OpenOption[] openOptions,
                                                            FileVisitOption[] fileVisitOption)
                                                     throws IOException
        Compares the file sets of two Paths to determine if they are equal or not while considering file contents. The comparison includes all files in all sub-directories.
        Parameters:
        path1 - The first directory.
        path2 - The second directory.
        linkOptions - options to follow links.
        openOptions - options to open files.
        fileVisitOption - options to configure traversal.
        Returns:
        Whether the two directories contain the same files while considering file contents.
        Throws:
        IOException - if an I/O error is thrown by a visitor method
      • directoryContentEquals

        public static boolean directoryContentEquals(Path path1,
                                                     Path path2)
                                              throws IOException
        Compares the file sets of two Paths to determine if they are equal or not without considering file contents. The comparison includes all files in all sub-directories.
        Parameters:
        path1 - The first directory.
        path2 - The second directory.
        Returns:
        Whether the two directories contain the same files without considering file contents.
        Throws:
        IOException - if an I/O error is thrown by a visitor method
      • directoryContentEquals

        public static boolean directoryContentEquals(Path path1,
                                                     Path path2,
                                                     int maxDepth,
                                                     LinkOption[] linkOptions,
                                                     FileVisitOption[] fileVisitOptions)
                                              throws IOException
        Compares the file sets of two Paths to determine if they are equal or not without considering file contents. The comparison includes all files in all sub-directories.
        Parameters:
        path1 - The first directory.
        path2 - The second directory.
        maxDepth - See Files.walkFileTree(Path,Set,int,FileVisitor).
        linkOptions - options to follow links.
        fileVisitOptions - options to configure the traversal
        Returns:
        Whether the two directories contain the same files without considering file contents.
        Throws:
        IOException - if an I/O error is thrown by a visitor method
      • filter

        public static Path[] filter(PathFilter filter,
                                    Path... paths)

        Applies an IOFileFilter to the provided File objects. The resulting array is a subset of the original file list that matches the provided filter.

        The Set returned by this method is not guaranteed to be thread safe.

         Set<File> allFiles = ...
         Set<File> javaFiles = FileFilterUtils.filterSet(allFiles,
             FileFilterUtils.suffixFileFilter(".java"));
         
        Parameters:
        filter - the filter to apply to the set of files.
        paths - the array of files to apply the filter to.
        Returns:
        a subset of files that is accepted by the file filter.
        Throws:
        IllegalArgumentException - if the filter is null or files contains a null value.
        Since:
        2.9.0
      • getAclEntryList

        public static List<AclEntry> getAclEntryList(Path sourcePath)
                                              throws IOException
        Reads the access control list from a file attribute view.
        Parameters:
        sourcePath - the path to the file.
        Returns:
        a file attribute view of the specified type, or null if the attribute view type is not available.
        Throws:
        IOException - if an I/O error occurs.
        Since:
        2.8.0
      • isDirectory

        public static boolean isDirectory(Path path,
                                          LinkOption... options)
        Tests whether the specified Path is a directory or not. Implemented as a null-safe delegate to Files.isDirectory(Path path, LinkOption... options).
        Parameters:
        path - the path to the file.
        options - options indicating how symbolic links are handled
        Returns:
        true if the file is a directory; false if the path is null, the file does not exist, is not a directory, or it cannot be determined if the file is a directory or not.
        Throws:
        SecurityException - In the case of the default provider, and a security manager is installed, the checkRead method is invoked to check read access to the directory.
        Since:
        2.9.0
      • isEmpty

        public static boolean isEmpty(Path path)
                               throws IOException
        Tests whether the given file or directory is empty.
        Parameters:
        path - the file or directory to query.
        Returns:
        whether the file or directory is empty.
        Throws:
        IOException - if an I/O error occurs.
      • isEmptyDirectory

        public static boolean isEmptyDirectory(Path directory)
                                        throws IOException
        Tests whether the directory is empty.
        Parameters:
        directory - the directory to query.
        Returns:
        whether the directory is empty.
        Throws:
        NotDirectoryException - if the file could not otherwise be opened because it is not a directory (optional specific exception).
        IOException - if an I/O error occurs.
        SecurityException - In the case of the default provider, and a security manager is installed, the checkRead method is invoked to check read access to the directory.
      • isEmptyFile

        public static boolean isEmptyFile(Path file)
                                   throws IOException
        Tests whether the given file is empty.
        Parameters:
        file - the file to query.
        Returns:
        whether the file is empty.
        Throws:
        IOException - if an I/O error occurs.
        SecurityException - In the case of the default provider, and a security manager is installed, its checkRead method denies read access to the file.
      • isNewer

        public static boolean isNewer(Path file,
                                      long timeMillis,
                                      LinkOption... options)
                               throws IOException
        Tests if the specified Path is newer than the specified time reference.
        Parameters:
        file - the Path of which the modification date must be compared
        timeMillis - the time reference measured in milliseconds since the epoch (00:00:00 GMT, January 1, 1970)
        options - options indicating how symbolic links are handled * @return true if the Path exists and has been modified after the given time reference.
        Returns:
        true if the Path exists and has been modified after the given time reference.
        Throws:
        IOException - if an I/O error occurs.
        NullPointerException - if the file is null
        Since:
        2.9.0
      • isRegularFile

        public static boolean isRegularFile(Path path,
                                            LinkOption... options)
        Tests whether the specified Path is a regular file or not. Implemented as a null-safe delegate to Files.isRegularFile(Path path, LinkOption... options).
        Parameters:
        path - the path to the file.
        options - options indicating how symbolic links are handled
        Returns:
        true if the file is a regular file; false if the path is null, the file does not exist, is not a directory, or it cannot be determined if the file is a regular file or not.
        Throws:
        SecurityException - In the case of the default provider, and a security manager is installed, the checkRead method is invoked to check read access to the directory.
        Since:
        2.9.0
      • newDirectoryStream

        public static DirectoryStream<Path> newDirectoryStream(Path dir,
                                                               PathFilter pathFilter)
                                                        throws IOException
        Creates a new DirectoryStream for Paths rooted at the given directory.
        Parameters:
        dir - the path to the directory to stream.
        pathFilter - the directory stream filter.
        Returns:
        a new instance.
        Throws:
        IOException - if an I/O error occurs.
      • readBasicFileAttributes

        public static BasicFileAttributes readBasicFileAttributes(Path path)
                                                           throws IOException
        Shorthand for Files.readAttributes(path, BasicFileAttributes.class)
        Parameters:
        path - the path to read.
        Returns:
        the path attributes.
        Throws:
        IOException - if an I/O error occurs.
        Since:
        2.9.0
      • setReadOnly

        public static Path setReadOnly(Path path,
                                       boolean readOnly,
                                       LinkOption... linkOptions)
                                throws IOException
        Sets the given Path to the readOnly value.

        This behavior is OS dependent.

        Parameters:
        path - The path to set.
        readOnly - true for read-only, false for not read-only.
        linkOptions - options indicating how symbolic links are handled.
        Returns:
        The given path.
        Throws:
        IOException - if an I/O error occurs.
        Since:
        2.8.0
      • walk

        public static Stream<Path> walk(Path start,
                                        PathFilter pathFilter,
                                        int maxDepth,
                                        boolean readAttributes,
                                        FileVisitOption... options)
                                 throws IOException
        Returns a stream of filtered paths.
        Parameters:
        start - the start path
        pathFilter - the path filter
        maxDepth - the maximum depth of directories to walk.
        readAttributes - whether to call the filters with file attributes (false passes null).
        options - the options to configure the walk.
        Returns:
        a filtered stream of paths.
        Throws:
        IOException - if an I/O error is thrown when accessing the starting file.
        Since:
        2.9.0

Copyright © 2002–2021 The Apache Software Foundation. All rights reserved.