Package org.apache.commons.io.file
Class PathUtils
- java.lang.Object
-
- org.apache.commons.io.file.PathUtils
-
public final class PathUtils extends java.lang.ObjectNIO Path utilities.- Since:
- 2.7
-
-
Field Summary
Fields Modifier and Type Field Description static DeleteOption[]EMPTY_DELETE_OPTION_ARRAYEmptyLinkOptionarray.static java.nio.file.FileVisitOption[]EMPTY_FILE_VISIT_OPTION_ARRAYEmptyFileVisitOptionarray.static java.nio.file.LinkOption[]EMPTY_LINK_OPTION_ARRAYEmptyLinkOptionarray.static java.nio.file.OpenOption[]EMPTY_OPEN_OPTION_ARRAYEmptyOpenOptionarray.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Counters.PathCounterscleanDirectory(java.nio.file.Path directory)Cleans a directory including sub-directories without deleting directories.static Counters.PathCounterscleanDirectory(java.nio.file.Path directory, DeleteOption... options)Cleans a directory including sub-directories without deleting directories.static Counters.PathCounterscopyDirectory(java.nio.file.Path sourceDirectory, java.nio.file.Path targetDirectory, java.nio.file.CopyOption... copyOptions)Copies a directory to another directory.static java.nio.file.PathcopyFile(java.net.URL sourceFile, java.nio.file.Path targetFile, java.nio.file.CopyOption... copyOptions)Copies a URL to a directory.static java.nio.file.PathcopyFileToDirectory(java.net.URL sourceFile, java.nio.file.Path targetDirectory, java.nio.file.CopyOption... copyOptions)Copies a URL to a directory.static java.nio.file.PathcopyFileToDirectory(java.nio.file.Path sourceFile, java.nio.file.Path targetDirectory, java.nio.file.CopyOption... copyOptions)Copies a file to a directory.static Counters.PathCounterscountDirectory(java.nio.file.Path directory)Counts aspects of a directory including sub-directories.static Counters.PathCountersdelete(java.nio.file.Path path)Deletes a file or directory.static Counters.PathCountersdelete(java.nio.file.Path path, DeleteOption... options)Deletes a file or directory.static Counters.PathCountersdeleteDirectory(java.nio.file.Path directory)Deletes a directory including sub-directories.static Counters.PathCountersdeleteDirectory(java.nio.file.Path directory, DeleteOption... options)Deletes a directory including sub-directories.static Counters.PathCountersdeleteFile(java.nio.file.Path file)Deletes the given file.static Counters.PathCountersdeleteFile(java.nio.file.Path file, DeleteOption... options)Deletes the given file.static booleandirectoryAndFileContentEquals(java.nio.file.Path path1, java.nio.file.Path path2)Compares the file sets of two Paths to determine if they are equal or not while considering file contents.static booleandirectoryAndFileContentEquals(java.nio.file.Path path1, java.nio.file.Path path2, java.nio.file.LinkOption[] linkOptions, java.nio.file.OpenOption[] openOptions, java.nio.file.FileVisitOption[] fileVisitOption)Compares the file sets of two Paths to determine if they are equal or not while considering file contents.static booleandirectoryContentEquals(java.nio.file.Path path1, java.nio.file.Path path2)Compares the file sets of two Paths to determine if they are equal or not without considering file contents.static booleandirectoryContentEquals(java.nio.file.Path path1, java.nio.file.Path path2, int maxDepth, java.nio.file.LinkOption[] linkOptions, java.nio.file.FileVisitOption[] fileVisitOptions)Compares the file sets of two Paths to determine if they are equal or not without considering file contents.static booleanfileContentEquals(java.nio.file.Path path1, java.nio.file.Path path2)Compares the file contents of two Paths to determine if they are equal or not.static booleanfileContentEquals(java.nio.file.Path path1, java.nio.file.Path path2, java.nio.file.LinkOption[] linkOptions, java.nio.file.OpenOption[] openOptions)Compares the file contents of two Paths to determine if they are equal or not.static java.util.List<java.nio.file.attribute.AclEntry>getAclEntryList(java.nio.file.Path sourcePath)Reads the access control list from a file attribute view.static booleanisEmpty(java.nio.file.Path path)Returns whether the given file or directory is empty.static booleanisEmptyDirectory(java.nio.file.Path directory)Returns whether the directory is empty.static booleanisEmptyFile(java.nio.file.Path file)Returns whether the given file is empty.static java.nio.file.PathsetReadOnly(java.nio.file.Path path, boolean readOnly, java.nio.file.LinkOption... options)Sets the given Path to thereadOnlyvalue.static <T extends java.nio.file.FileVisitor<? super java.nio.file.Path>>
TvisitFileTree(T visitor, java.lang.String first, java.lang.String... more)PerformsFiles.walkFileTree(Path,FileVisitor)and returns the given visitor.static <T extends java.nio.file.FileVisitor<? super java.nio.file.Path>>
TvisitFileTree(T visitor, java.net.URI uri)PerformsFiles.walkFileTree(Path,FileVisitor)and returns the given visitor.static <T extends java.nio.file.FileVisitor<? super java.nio.file.Path>>
TvisitFileTree(T visitor, java.nio.file.Path directory)PerformsFiles.walkFileTree(Path,FileVisitor)and returns the given visitor.static <T extends java.nio.file.FileVisitor<? super java.nio.file.Path>>
TvisitFileTree(T visitor, java.nio.file.Path start, java.util.Set<java.nio.file.FileVisitOption> options, int maxDepth)PerformsFiles.walkFileTree(Path,FileVisitor)and returns the given visitor.
-
-
-
Field Detail
-
EMPTY_DELETE_OPTION_ARRAY
public static final DeleteOption[] EMPTY_DELETE_OPTION_ARRAY
EmptyLinkOptionarray.- Since:
- 2.8.0
-
EMPTY_FILE_VISIT_OPTION_ARRAY
public static final java.nio.file.FileVisitOption[] EMPTY_FILE_VISIT_OPTION_ARRAY
EmptyFileVisitOptionarray.
-
EMPTY_LINK_OPTION_ARRAY
public static final java.nio.file.LinkOption[] EMPTY_LINK_OPTION_ARRAY
EmptyLinkOptionarray.
-
EMPTY_OPEN_OPTION_ARRAY
public static final java.nio.file.OpenOption[] EMPTY_OPEN_OPTION_ARRAY
EmptyOpenOptionarray.
-
-
Method Detail
-
cleanDirectory
public static Counters.PathCounters cleanDirectory(java.nio.file.Path directory) throws java.io.IOException
Cleans a directory including sub-directories without deleting directories.- Parameters:
directory- directory to clean.- Returns:
- The visitation path counters.
- Throws:
java.io.IOException- if an I/O error is thrown by a visitor method.
-
cleanDirectory
public static Counters.PathCounters cleanDirectory(java.nio.file.Path directory, DeleteOption... options) throws java.io.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:
java.io.IOException- if an I/O error is thrown by a visitor method.- Since:
- 2.8.0
-
copyDirectory
public static Counters.PathCounters copyDirectory(java.nio.file.Path sourceDirectory, java.nio.file.Path targetDirectory, java.nio.file.CopyOption... copyOptions) throws java.io.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:
java.io.IOException- if an I/O error is thrown by a visitor method.
-
copyFile
public static java.nio.file.Path copyFile(java.net.URL sourceFile, java.nio.file.Path targetFile, java.nio.file.CopyOption... copyOptions) throws java.io.IOExceptionCopies 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:
java.io.IOException- if an I/O error occurs- See Also:
Files.copy(InputStream, Path, CopyOption...)
-
copyFileToDirectory
public static java.nio.file.Path copyFileToDirectory(java.nio.file.Path sourceFile, java.nio.file.Path targetDirectory, java.nio.file.CopyOption... copyOptions) throws java.io.IOExceptionCopies 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:
java.io.IOException- if an I/O error occurs- See Also:
Files.copy(Path, Path, CopyOption...)
-
copyFileToDirectory
public static java.nio.file.Path copyFileToDirectory(java.net.URL sourceFile, java.nio.file.Path targetDirectory, java.nio.file.CopyOption... copyOptions) throws java.io.IOExceptionCopies 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:
java.io.IOException- if an I/O error occurs- See Also:
Files.copy(InputStream, Path, CopyOption...)
-
countDirectory
public static Counters.PathCounters countDirectory(java.nio.file.Path directory) throws java.io.IOException
Counts aspects of a directory including sub-directories.- Parameters:
directory- directory to delete.- Returns:
- The visitor used to count the given directory.
- Throws:
java.io.IOException- if an I/O error is thrown by a visitor method.
-
delete
public static Counters.PathCounters delete(java.nio.file.Path path) throws java.io.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 benull- Returns:
- The visitor used to delete the given directory.
- Throws:
java.lang.NullPointerException- if the directory isnulljava.io.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(java.nio.file.Path path, DeleteOption... options) throws java.io.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 benulloptions- options indicating how deletion is handled.- Returns:
- The visitor used to delete the given directory.
- Throws:
java.lang.NullPointerException- if the directory isnulljava.io.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(java.nio.file.Path directory) throws java.io.IOException
Deletes a directory including sub-directories.- Parameters:
directory- directory to delete.- Returns:
- The visitor used to delete the given directory.
- Throws:
java.io.IOException- if an I/O error is thrown by a visitor method.
-
deleteDirectory
public static Counters.PathCounters deleteDirectory(java.nio.file.Path directory, DeleteOption... options) throws java.io.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:
java.io.IOException- if an I/O error is thrown by a visitor method.- Since:
- 2.8.0
-
deleteFile
public static Counters.PathCounters deleteFile(java.nio.file.Path file) throws java.io.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:
java.io.IOException- if an I/O error occurs.java.nio.file.NoSuchFileException- if the file is a directory.
-
deleteFile
public static Counters.PathCounters deleteFile(java.nio.file.Path file, DeleteOption... options) throws java.io.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:
java.io.IOException- if an I/O error occurs.java.nio.file.NoSuchFileException- if the file is a directory.- Since:
- 2.8.0
-
directoryAndFileContentEquals
public static boolean directoryAndFileContentEquals(java.nio.file.Path path1, java.nio.file.Path path2) throws java.io.IOExceptionCompares 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:
java.io.IOException- if an I/O error is thrown by a visitor method
-
directoryAndFileContentEquals
public static boolean directoryAndFileContentEquals(java.nio.file.Path path1, java.nio.file.Path path2, java.nio.file.LinkOption[] linkOptions, java.nio.file.OpenOption[] openOptions, java.nio.file.FileVisitOption[] fileVisitOption) throws java.io.IOExceptionCompares 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:
java.io.IOException- if an I/O error is thrown by a visitor method
-
directoryContentEquals
public static boolean directoryContentEquals(java.nio.file.Path path1, java.nio.file.Path path2) throws java.io.IOExceptionCompares 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:
java.io.IOException- if an I/O error is thrown by a visitor method
-
directoryContentEquals
public static boolean directoryContentEquals(java.nio.file.Path path1, java.nio.file.Path path2, int maxDepth, java.nio.file.LinkOption[] linkOptions, java.nio.file.FileVisitOption[] fileVisitOptions) throws java.io.IOExceptionCompares 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- SeeFiles.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:
java.io.IOException- if an I/O error is thrown by a visitor method
-
fileContentEquals
public static boolean fileContentEquals(java.nio.file.Path path1, java.nio.file.Path path2) throws java.io.IOExceptionCompares 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:
java.lang.NullPointerException- if either input is null.java.io.IOException- if an I/O error occurs.- See Also:
FileUtils.contentEquals(java.io.File, java.io.File)
-
fileContentEquals
public static boolean fileContentEquals(java.nio.file.Path path1, java.nio.file.Path path2, java.nio.file.LinkOption[] linkOptions, java.nio.file.OpenOption[] openOptions) throws java.io.IOExceptionCompares 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:
java.lang.NullPointerException- if either input is null.java.io.IOException- if an I/O error occurs.- See Also:
FileUtils.contentEquals(java.io.File, java.io.File)
-
getAclEntryList
public static java.util.List<java.nio.file.attribute.AclEntry> getAclEntryList(java.nio.file.Path sourcePath) throws java.io.IOExceptionReads 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:
java.io.IOException- if an I/O error occurs.- Since:
- 2.8.0
-
isEmpty
public static boolean isEmpty(java.nio.file.Path path) throws java.io.IOExceptionReturns 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:
java.io.IOException- if an I/O error occurs
-
isEmptyDirectory
public static boolean isEmptyDirectory(java.nio.file.Path directory) throws java.io.IOExceptionReturns whether the directory is empty.- Parameters:
directory- the the given directory to query.- Returns:
- whether the given directory is empty.
- Throws:
java.io.IOException- if an I/O error occurs
-
isEmptyFile
public static boolean isEmptyFile(java.nio.file.Path file) throws java.io.IOExceptionReturns whether the given file is empty.- Parameters:
file- the the given file to query.- Returns:
- whether the given file is empty.
- Throws:
java.io.IOException- if an I/O error occurs
-
setReadOnly
public static java.nio.file.Path setReadOnly(java.nio.file.Path path, boolean readOnly, java.nio.file.LinkOption... options) throws java.io.IOExceptionSets the given Path to thereadOnlyvalue.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:
java.io.IOException- if an I/O error occurs.- Since:
- 2.8.0
-
visitFileTree
public static <T extends java.nio.file.FileVisitor<? super java.nio.file.Path>> T visitFileTree(T visitor, java.nio.file.Path directory) throws java.io.IOExceptionPerformsFiles.walkFileTree(Path,FileVisitor)and returns the given visitor. Note thatFiles.walkFileTree(Path,FileVisitor)returns the given path.- Type Parameters:
T- SeeFiles.walkFileTree(Path,FileVisitor).- Parameters:
visitor- SeeFiles.walkFileTree(Path,FileVisitor).directory- SeeFiles.walkFileTree(Path,FileVisitor).- Returns:
- the given visitor.
- Throws:
java.io.IOException- if an I/O error is thrown by a visitor method
-
visitFileTree
public static <T extends java.nio.file.FileVisitor<? super java.nio.file.Path>> T visitFileTree(T visitor, java.nio.file.Path start, java.util.Set<java.nio.file.FileVisitOption> options, int maxDepth) throws java.io.IOExceptionPerformsFiles.walkFileTree(Path,FileVisitor)and returns the given visitor. Note thatFiles.walkFileTree(Path,FileVisitor)returns the given path.- Type Parameters:
T- SeeFiles.walkFileTree(Path,Set,int,FileVisitor).- Parameters:
start- SeeFiles.walkFileTree(Path,Set,int,FileVisitor).options- SeeFiles.walkFileTree(Path,Set,int,FileVisitor).maxDepth- SeeFiles.walkFileTree(Path,Set,int,FileVisitor).visitor- SeeFiles.walkFileTree(Path,Set,int,FileVisitor).- Returns:
- the given visitor.
- Throws:
java.io.IOException- if an I/O error is thrown by a visitor method
-
visitFileTree
public static <T extends java.nio.file.FileVisitor<? super java.nio.file.Path>> T visitFileTree(T visitor, java.lang.String first, java.lang.String... more) throws java.io.IOExceptionPerformsFiles.walkFileTree(Path,FileVisitor)and returns the given visitor. Note thatFiles.walkFileTree(Path,FileVisitor)returns the given path.- Type Parameters:
T- SeeFiles.walkFileTree(Path,FileVisitor).- Parameters:
visitor- SeeFiles.walkFileTree(Path,FileVisitor).first- SeePaths.get(String,String[]).more- SeePaths.get(String,String[]).- Returns:
- the given visitor.
- Throws:
java.io.IOException- if an I/O error is thrown by a visitor method
-
visitFileTree
public static <T extends java.nio.file.FileVisitor<? super java.nio.file.Path>> T visitFileTree(T visitor, java.net.URI uri) throws java.io.IOExceptionPerformsFiles.walkFileTree(Path,FileVisitor)and returns the given visitor. Note thatFiles.walkFileTree(Path,FileVisitor)returns the given path.- Type Parameters:
T- SeeFiles.walkFileTree(Path,FileVisitor).- Parameters:
visitor- SeeFiles.walkFileTree(Path,FileVisitor).uri- SeePaths.get(URI).- Returns:
- the given visitor.
- Throws:
java.io.IOException- if an I/O error is thrown by a visitor method
-
-