Class PathUtils

java.lang.Object
org.apache.commons.io.file.PathUtils

public final class PathUtils extends Object
NIO Path utilities.
Since:
2.7
  • Field Details

    • EMPTY_DELETE_OPTION_ARRAY

      public static final DeleteOption[] EMPTY_DELETE_OPTION_ARRAY
      Empty LinkOption array.
      Since:
      2.8.0
    • EMPTY_FILE_VISIT_OPTION_ARRAY

      public static final FileVisitOption[] EMPTY_FILE_VISIT_OPTION_ARRAY
      Empty FileVisitOption array.
    • EMPTY_OPEN_OPTION_ARRAY

      public static final OpenOption[] EMPTY_OPEN_OPTION_ARRAY
      Empty OpenOption array.
  • Constructor Details

    • PathUtils

      private PathUtils()
      Does allow to instantiate.
  • Method Details

    • accumulate

      private static AccumulatorPathVisitor accumulate(Path directory, int maxDepth, FileVisitOption[] fileVisitOptions) throws IOException
      Accumulates file tree information in a AccumulatorPathVisitor.
      Parameters:
      directory - The directory to accumulate information.
      maxDepth - See Files.walkFileTree(Path,Set,int,FileVisitor).
      fileVisitOptions - See Files.walkFileTree(Path,Set,int,FileVisitor).
      Returns:
      file tree information.
      Throws:
      IOException - if an I/O error is thrown by a visitor method.
    • 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... options) throws IOException
      Cleans a directory including sub-directories without deleting directories.
      Parameters:
      directory - directory to clean.
      options - options indicating how deletion is handled.
      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.
    • copyFile

      public static Path copyFile(URL sourceFile, Path targetFile, CopyOption... copyOptions) throws IOException
      Copies a URL to a directory.
      Parameters:
      sourceFile - The source URL.
      targetFile - The target file.
      copyOptions - Specifies how the copying should be done.
      Returns:
      The target file
      Throws:
      IOException - if an I/O error occurs
      See Also:
    • 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:
    • 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:
    • 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.
    • 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... options) 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
      options - options indicating how deletion is handled.
      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
    • 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... options) throws IOException
      Deletes a directory including sub-directories.
      Parameters:
      directory - directory to delete.
      options - options indicating how deletion is handled.
      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
    • 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... options) throws IOException
      Deletes the given file.
      Parameters:
      file - The file to delete.
      options - options indicating how deletion is handled.
      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
    • overrideReadOnly

      private static boolean overrideReadOnly(DeleteOption[] options)
      Returns true if the given options contain StandardDeleteOption.OVERRIDE_READ_ONLY.
      Parameters:
      options - the array to test
      Returns:
      true if the given options contain StandardDeleteOption.OVERRIDE_READ_ONLY.
    • 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
    • fileContentEquals

      public static boolean fileContentEquals(Path path1, Path path2) throws IOException
      Compares the file contents of two Paths to determine if they are equal or not.

      File content is accessed through Files.newInputStream(Path,OpenOption...).

      Parameters:
      path1 - the first stream.
      path2 - the second stream.
      Returns:
      true if the content of the streams are equal or they both don't exist, false otherwise.
      Throws:
      NullPointerException - if either input is null.
      IOException - if an I/O error occurs.
      See Also:
    • fileContentEquals

      public static boolean fileContentEquals(Path path1, Path path2, LinkOption[] linkOptions, OpenOption[] openOptions) throws IOException
      Compares the file contents of two Paths to determine if they are equal or not.

      File content is accessed through Files.newInputStream(Path,OpenOption...).

      Parameters:
      path1 - the first stream.
      path2 - the second stream.
      linkOptions - options specifying how files are followed.
      openOptions - options specifying how files are opened.
      Returns:
      true if the content of the streams are equal or they both don't exist, false otherwise.
      Throws:
      NullPointerException - if either input is null.
      IOException - if an I/O error occurs.
      See Also:
    • 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 ifthe attribute view type is not available.
      Throws:
      IOException - if an I/O error occurs.
      Since:
      2.8.0
    • isEmpty

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

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

      public static boolean isEmptyFile(Path file) throws IOException
      Returns whether the given file is empty.
      Parameters:
      file - the the given file to query.
      Returns:
      whether the given file is empty.
      Throws:
      IOException - if an I/O error occurs
    • relativize

      static List<Path> relativize(Collection<Path> collection, Path parent, boolean sort, Comparator<? super Path> comparator)
      Relativizes all files in the given collection against a parent.
      Parameters:
      collection - The collection of paths to relativize.
      parent - relativizes against this parent path.
      sort - Whether to sort the result.
      comparator - How to sort.
      Returns:
      A collection of relativized paths, optionally sorted.
    • setReadOnly

      public static Path setReadOnly(Path path, boolean readOnly, LinkOption... options) 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.
      options - options indicating how symbolic links are handled.
      Returns:
      The given path.
      Throws:
      IOException - if an I/O error occurs.
      Since:
      2.8.0
    • toFileVisitOptionSet

      static Set<FileVisitOption> toFileVisitOptionSet(FileVisitOption... fileVisitOptions)
      Converts an array of FileVisitOption to a Set.
      Parameters:
      fileVisitOptions - input array.
      Returns:
      a new Set.
    • visitFileTree

      public static <T extends FileVisitor<? super Path>> T visitFileTree(T visitor, Path directory) throws IOException
      Performs Files.walkFileTree(Path,FileVisitor) and returns the given visitor. Note that Files.walkFileTree(Path,FileVisitor) returns the given path.
      Type Parameters:
      T - See Files.walkFileTree(Path,FileVisitor).
      Parameters:
      visitor - See Files.walkFileTree(Path,FileVisitor).
      directory - See Files.walkFileTree(Path,FileVisitor).
      Returns:
      the given visitor.
      Throws:
      IOException - if an I/O error is thrown by a visitor method
    • visitFileTree

      public static <T extends FileVisitor<? super Path>> T visitFileTree(T visitor, Path start, Set<FileVisitOption> options, int maxDepth) throws IOException
      Performs Files.walkFileTree(Path,FileVisitor) and returns the given visitor. Note that Files.walkFileTree(Path,FileVisitor) returns the given path.
      Type Parameters:
      T - See Files.walkFileTree(Path,Set,int,FileVisitor).
      Parameters:
      start - See Files.walkFileTree(Path,Set,int,FileVisitor).
      options - See Files.walkFileTree(Path,Set,int,FileVisitor).
      maxDepth - See Files.walkFileTree(Path,Set,int,FileVisitor).
      visitor - See Files.walkFileTree(Path,Set,int,FileVisitor).
      Returns:
      the given visitor.
      Throws:
      IOException - if an I/O error is thrown by a visitor method
    • visitFileTree

      public static <T extends FileVisitor<? super Path>> T visitFileTree(T visitor, String first, String... more) throws IOException
      Performs Files.walkFileTree(Path,FileVisitor) and returns the given visitor. Note that Files.walkFileTree(Path,FileVisitor) returns the given path.
      Type Parameters:
      T - See Files.walkFileTree(Path,FileVisitor).
      Parameters:
      visitor - See Files.walkFileTree(Path,FileVisitor).
      first - See Paths.get(String,String[]).
      more - See Paths.get(String,String[]).
      Returns:
      the given visitor.
      Throws:
      IOException - if an I/O error is thrown by a visitor method
    • visitFileTree

      public static <T extends FileVisitor<? super Path>> T visitFileTree(T visitor, URI uri) throws IOException
      Performs Files.walkFileTree(Path,FileVisitor) and returns the given visitor. Note that Files.walkFileTree(Path,FileVisitor) returns the given path.
      Type Parameters:
      T - See Files.walkFileTree(Path,FileVisitor).
      Parameters:
      visitor - See Files.walkFileTree(Path,FileVisitor).
      uri - See Paths.get(URI).
      Returns:
      the given visitor.
      Throws:
      IOException - if an I/O error is thrown by a visitor method