public final class PathUtils extends Object
Modifier and Type | Field and Description |
---|---|
static CopyOption[] |
EMPTY_COPY_OPTIONS
Empty
CopyOption array. |
static DeleteOption[] |
EMPTY_DELETE_OPTION_ARRAY
Empty
LinkOption array. |
static FileVisitOption[] |
EMPTY_FILE_VISIT_OPTION_ARRAY
Empty
FileVisitOption array. |
static LinkOption[] |
EMPTY_LINK_OPTION_ARRAY
Empty
LinkOption array. |
static OpenOption[] |
EMPTY_OPEN_OPTION_ARRAY
Empty
OpenOption array. |
static Path[] |
EMPTY_PATH_ARRAY
Empty
Path array. |
static LinkOption[] |
NOFOLLOW_LINK_OPTION_ARRAY
LinkOption array for LinkOption.NOFOLLOW_LINKS . |
Modifier and Type | Method and Description |
---|---|
static Counters.PathCounters |
cleanDirectory(Path directory)
Cleans a directory including sub-directories without deleting directories.
|
static Counters.PathCounters |
cleanDirectory(Path directory,
DeleteOption... deleteOptions)
Cleans a directory including sub-directories without deleting directories.
|
static Counters.PathCounters |
copyDirectory(Path sourceDirectory,
Path targetDirectory,
CopyOption... copyOptions)
Copies a directory to another directory.
|
static Path |
copyFile(URL sourceFile,
Path targetFile,
CopyOption... copyOptions)
Copies a URL to a directory.
|
static Path |
copyFileToDirectory(Path sourceFile,
Path targetDirectory,
CopyOption... copyOptions)
Copies a file to a directory.
|
static Path |
copyFileToDirectory(URL sourceFile,
Path targetDirectory,
CopyOption... copyOptions)
Copies a URL to a directory.
|
static Counters.PathCounters |
countDirectory(Path directory)
Counts aspects of a directory including sub-directories.
|
static Path |
createParentDirectories(Path path,
FileAttribute<?>... attrs)
Creates the parent directories for the given
path . |
static Path |
current()
Gets the current directory.
|
static Counters.PathCounters |
delete(Path path)
Deletes a file or directory.
|
static Counters.PathCounters |
delete(Path path,
DeleteOption... deleteOptions)
Deletes a file or directory.
|
static Counters.PathCounters |
delete(Path path,
LinkOption[] linkOptions,
DeleteOption... deleteOptions)
Deletes a file or directory.
|
static Counters.PathCounters |
deleteDirectory(Path directory)
Deletes a directory including sub-directories.
|
static Counters.PathCounters |
deleteDirectory(Path directory,
DeleteOption... deleteOptions)
Deletes a directory including sub-directories.
|
static Counters.PathCounters |
deleteDirectory(Path directory,
LinkOption[] linkOptions,
DeleteOption... deleteOptions)
Deletes a directory including sub-directories.
|
static Counters.PathCounters |
deleteFile(Path file)
Deletes the given file.
|
static Counters.PathCounters |
deleteFile(Path file,
DeleteOption... deleteOptions)
Deletes the given file.
|
static Counters.PathCounters |
deleteFile(Path file,
LinkOption[] linkOptions,
DeleteOption... deleteOptions)
Deletes the given file.
|
static boolean |
directoryAndFileContentEquals(Path path1,
Path path2)
Compares the file sets of two Paths to determine if they are equal or not while considering file contents.
|
static boolean |
directoryAndFileContentEquals(Path path1,
Path path2,
LinkOption[] linkOptions,
OpenOption[] openOptions,
FileVisitOption[] fileVisitOption)
Compares the file sets of two Paths to determine if they are equal or not while considering file contents.
|
static boolean |
directoryContentEquals(Path path1,
Path path2)
Compares the file sets of two Paths to determine if they are equal or not without considering file contents.
|
static boolean |
directoryContentEquals(Path path1,
Path path2,
int maxDepth,
LinkOption[] linkOptions,
FileVisitOption[] fileVisitOptions)
Compares the file sets of two Paths to determine if they are equal or not without considering file contents.
|
static boolean |
fileContentEquals(Path path1,
Path path2)
Compares the file contents of two Paths to determine if they are equal or not.
|
static boolean |
fileContentEquals(Path path1,
Path path2,
LinkOption[] linkOptions,
OpenOption[] openOptions)
Compares the file contents of two Paths to determine if they are equal or not.
|
static Path[] |
filter(PathFilter filter,
Path... paths)
Applies an
IOFileFilter to the provided File objects. |
static List<AclEntry> |
getAclEntryList(Path sourcePath)
Reads the access control list from a file attribute view.
|
static boolean |
isDirectory(Path path,
LinkOption... options)
Tests whether the specified
Path is a directory or not. |
static boolean |
isEmpty(Path path)
Tests whether the given file or directory is empty.
|
static boolean |
isEmptyDirectory(Path directory)
Tests whether the directory is empty.
|
static boolean |
isEmptyFile(Path file)
Tests whether the given file is empty.
|
static boolean |
isNewer(Path file,
long timeMillis,
LinkOption... options)
Tests if the specified
Path is newer than the specified time reference. |
static boolean |
isRegularFile(Path path,
LinkOption... options)
Tests whether the specified
Path is a regular file or not. |
static DirectoryStream<Path> |
newDirectoryStream(Path dir,
PathFilter pathFilter)
Creates a new DirectoryStream for Paths rooted at the given directory.
|
static BasicFileAttributes |
readBasicFileAttributes(Path path)
Shorthand for
Files.readAttributes(path, BasicFileAttributes.class) |
static BasicFileAttributes |
readBasicFileAttributesUnchecked(Path path)
Shorthand for
Files.readAttributes(path, BasicFileAttributes.class) while wrapping IOException
as UncheckedIOException . |
static Path |
setReadOnly(Path path,
boolean readOnly,
LinkOption... linkOptions)
Sets the given Path to the
readOnly value. |
static <T extends FileVisitor<? super Path>> |
visitFileTree(T visitor,
Path directory)
Performs
Files.walkFileTree(Path,FileVisitor) and returns the given visitor. |
static <T extends FileVisitor<? super Path>> |
visitFileTree(T visitor,
Path start,
Set<FileVisitOption> options,
int maxDepth)
Performs
Files.walkFileTree(Path,FileVisitor) and returns the given visitor. |
static <T extends FileVisitor<? super Path>> |
visitFileTree(T visitor,
String first,
String... more)
Performs
Files.walkFileTree(Path,FileVisitor) and returns the given visitor. |
static <T extends FileVisitor<? super Path>> |
visitFileTree(T visitor,
URI uri)
Performs
Files.walkFileTree(Path,FileVisitor) and returns the given visitor. |
static Stream<Path> |
walk(Path start,
PathFilter pathFilter,
int maxDepth,
boolean readAttributes,
FileVisitOption... options)
Returns a stream of filtered paths.
|
public static final CopyOption[] EMPTY_COPY_OPTIONS
CopyOption
array.public static final DeleteOption[] EMPTY_DELETE_OPTION_ARRAY
LinkOption
array.public static final FileVisitOption[] EMPTY_FILE_VISIT_OPTION_ARRAY
FileVisitOption
array.public static final LinkOption[] EMPTY_LINK_OPTION_ARRAY
LinkOption
array.public static final LinkOption[] NOFOLLOW_LINK_OPTION_ARRAY
LinkOption
array for LinkOption.NOFOLLOW_LINKS
.public static final OpenOption[] EMPTY_OPEN_OPTION_ARRAY
OpenOption
array.public static Counters.PathCounters cleanDirectory(Path directory) throws IOException
directory
- directory to clean.IOException
- if an I/O error is thrown by a visitor method.public static Counters.PathCounters cleanDirectory(Path directory, DeleteOption... deleteOptions) throws IOException
directory
- directory to clean.deleteOptions
- How to handle deletion.IOException
- if an I/O error is thrown by a visitor method.public static Counters.PathCounters copyDirectory(Path sourceDirectory, Path targetDirectory, CopyOption... copyOptions) throws IOException
sourceDirectory
- The source directory.targetDirectory
- The target directory.copyOptions
- Specifies how the copying should be done.IOException
- if an I/O error is thrown by a visitor method.public static Path copyFile(URL sourceFile, Path targetFile, CopyOption... copyOptions) throws IOException
sourceFile
- The source URL.targetFile
- The target file.copyOptions
- Specifies how the copying should be done.IOException
- if an I/O error occurs.Files.copy(InputStream, Path, CopyOption...)
public static Path copyFileToDirectory(Path sourceFile, Path targetDirectory, CopyOption... copyOptions) throws IOException
sourceFile
- The source file.targetDirectory
- The target directory.copyOptions
- Specifies how the copying should be done.IOException
- if an I/O error occurs.Files.copy(Path, Path, CopyOption...)
public static Path copyFileToDirectory(URL sourceFile, Path targetDirectory, CopyOption... copyOptions) throws IOException
sourceFile
- The source URL.targetDirectory
- The target directory.copyOptions
- Specifies how the copying should be done.IOException
- if an I/O error occurs.Files.copy(InputStream, Path, CopyOption...)
public static Counters.PathCounters countDirectory(Path directory) throws IOException
directory
- directory to delete.IOException
- if an I/O error is thrown by a visitor method.public static Path createParentDirectories(Path path, FileAttribute<?>... attrs) throws IOException
path
.path
- The path to a file (or directory).attrs
- An optional list of file attributes to set atomically when creating the directories.path
's parent directory or null if the given path has no parent.IOException
- if an I/O error occurs.public static Path current()
public static Counters.PathCounters delete(Path path) throws IOException
The difference between File.delete() and this method are:
File.delete()
returns a
boolean.
path
- file or directory to delete, must not be null
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.public static Counters.PathCounters delete(Path path, DeleteOption... deleteOptions) throws IOException
The difference between File.delete() and this method are:
File.delete()
returns a
boolean.
path
- file or directory to delete, must not be null
deleteOptions
- How to handle deletion.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.public static Counters.PathCounters delete(Path path, LinkOption[] linkOptions, DeleteOption... deleteOptions) throws IOException
The difference between File.delete() and this method are:
File.delete()
returns a
boolean.
path
- file or directory to delete, must not be null
linkOptions
- How to handle symbolic links.deleteOptions
- How to handle deletion.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.public static Counters.PathCounters deleteDirectory(Path directory) throws IOException
directory
- directory to delete.IOException
- if an I/O error is thrown by a visitor method.public static Counters.PathCounters deleteDirectory(Path directory, DeleteOption... deleteOptions) throws IOException
directory
- directory to delete.deleteOptions
- How to handle deletion.IOException
- if an I/O error is thrown by a visitor method.public static Counters.PathCounters deleteDirectory(Path directory, LinkOption[] linkOptions, DeleteOption... deleteOptions) throws IOException
directory
- directory to delete.linkOptions
- How to handle symbolic links.deleteOptions
- How to handle deletion.IOException
- if an I/O error is thrown by a visitor method.public static Counters.PathCounters deleteFile(Path file) throws IOException
file
- The file to delete.IOException
- if an I/O error occurs.NoSuchFileException
- if the file is a directory.public static Counters.PathCounters deleteFile(Path file, DeleteOption... deleteOptions) throws IOException
file
- The file to delete.deleteOptions
- How to handle deletion.IOException
- if an I/O error occurs.NoSuchFileException
- if the file is a directory.public static Counters.PathCounters deleteFile(Path file, LinkOption[] linkOptions, DeleteOption... deleteOptions) throws NoSuchFileException, IOException
file
- The file to delete.linkOptions
- How to handle symbolic links.deleteOptions
- How to handle deletion.IOException
- if an I/O error occurs.NoSuchFileException
- if the file is a directory.public static boolean directoryAndFileContentEquals(Path path1, Path path2) throws IOException
path1
- The first directory.path2
- The second directory.IOException
- if an I/O error is thrown by a visitor methodpublic static boolean directoryAndFileContentEquals(Path path1, Path path2, LinkOption[] linkOptions, OpenOption[] openOptions, FileVisitOption[] fileVisitOption) throws IOException
path1
- The first directory.path2
- The second directory.linkOptions
- options to follow links.openOptions
- options to open files.fileVisitOption
- options to configure traversal.IOException
- if an I/O error is thrown by a visitor methodpublic static boolean directoryContentEquals(Path path1, Path path2) throws IOException
path1
- The first directory.path2
- The second directory.IOException
- if an I/O error is thrown by a visitor methodpublic static boolean directoryContentEquals(Path path1, Path path2, int maxDepth, LinkOption[] linkOptions, FileVisitOption[] fileVisitOptions) throws IOException
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 traversalIOException
- if an I/O error is thrown by a visitor methodpublic static boolean fileContentEquals(Path path1, Path path2) throws IOException
File content is accessed through Files.newInputStream(Path,OpenOption...)
.
path1
- the first stream.path2
- the second stream.NullPointerException
- if either input is null.IOException
- if an I/O error occurs.FileUtils.contentEquals(java.io.File, java.io.File)
public static boolean fileContentEquals(Path path1, Path path2, LinkOption[] linkOptions, OpenOption[] openOptions) throws IOException
File content is accessed through Files.newInputStream(Path,OpenOption...)
.
path1
- the first stream.path2
- the second stream.linkOptions
- options specifying how files are followed.openOptions
- options specifying how files are opened.NullPointerException
- if either input is null.IOException
- if an I/O error occurs.FileUtils.contentEquals(java.io.File, java.io.File)
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"));
filter
- the filter to apply to the set of files.paths
- the array of files to apply the filter to.files
that is accepted by the file filter.IllegalArgumentException
- if the filter is null
or files
contains a null
value.public static List<AclEntry> getAclEntryList(Path sourcePath) throws IOException
sourcePath
- the path to the file.IOException
- if an I/O error occurs.public static boolean isDirectory(Path path, LinkOption... options)
Path
is a directory or not. Implemented as a
null-safe delegate to Files.isDirectory(Path path, LinkOption... options)
.path
- the path to the file.options
- options indicating how symbolic links are handledtrue
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.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.public static boolean isEmpty(Path path) throws IOException
path
- the file or directory to query.IOException
- if an I/O error occurs.public static boolean isEmptyDirectory(Path directory) throws IOException
directory
- the directory to query.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.public static boolean isEmptyFile(Path file) throws IOException
file
- the file to query.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.public static boolean isNewer(Path file, long timeMillis, LinkOption... options) throws IOException
Path
is newer than the specified time reference.file
- the Path
of which the modification date must be comparedtimeMillis
- 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.Path
exists and has been modified after the given time reference.IOException
- if an I/O error occurs.NullPointerException
- if the file is null
public static boolean isRegularFile(Path path, LinkOption... options)
Path
is a regular file or not. Implemented as a
null-safe delegate to Files.isRegularFile(Path path, LinkOption... options)
.path
- the path to the file.options
- options indicating how symbolic links are handledtrue
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.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.public static DirectoryStream<Path> newDirectoryStream(Path dir, PathFilter pathFilter) throws IOException
dir
- the path to the directory to stream.pathFilter
- the directory stream filter.IOException
- if an I/O error occurs.public static BasicFileAttributes readBasicFileAttributes(Path path) throws IOException
Files.readAttributes(path, BasicFileAttributes.class)
path
- the path to read.IOException
- if an I/O error occurs.public static BasicFileAttributes readBasicFileAttributesUnchecked(Path path)
Files.readAttributes(path, BasicFileAttributes.class)
while wrapping IOException
as UncheckedIOException
.path
- the path to read.UncheckedIOException
- if an I/O error occurspublic static Path setReadOnly(Path path, boolean readOnly, LinkOption... linkOptions) throws IOException
readOnly
value.
This behavior is OS dependent.
path
- The path to set.readOnly
- true for read-only, false for not read-only.linkOptions
- options indicating how symbolic links are handled.IOException
- if an I/O error occurs.public static <T extends FileVisitor<? super Path>> T visitFileTree(T visitor, Path directory) throws IOException
Files.walkFileTree(Path,FileVisitor)
and returns the given visitor.
Note that Files.walkFileTree(Path,FileVisitor)
returns the given path.T
- See Files.walkFileTree(Path,FileVisitor)
.visitor
- See Files.walkFileTree(Path,FileVisitor)
.directory
- See Files.walkFileTree(Path,FileVisitor)
.IOException
- if an I/O error is thrown by a visitor methodpublic static <T extends FileVisitor<? super Path>> T visitFileTree(T visitor, Path start, Set<FileVisitOption> options, int maxDepth) throws IOException
Files.walkFileTree(Path,FileVisitor)
and returns the given visitor.
Note that Files.walkFileTree(Path,FileVisitor)
returns the given path.T
- See Files.walkFileTree(Path,Set,int,FileVisitor)
.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)
.IOException
- if an I/O error is thrown by a visitor methodpublic static <T extends FileVisitor<? super Path>> T visitFileTree(T visitor, String first, String... more) throws IOException
Files.walkFileTree(Path,FileVisitor)
and returns the given visitor.
Note that Files.walkFileTree(Path,FileVisitor)
returns the given path.T
- See Files.walkFileTree(Path,FileVisitor)
.visitor
- See Files.walkFileTree(Path,FileVisitor)
.first
- See Paths.get(String,String[])
.more
- See Paths.get(String,String[])
.IOException
- if an I/O error is thrown by a visitor methodpublic static <T extends FileVisitor<? super Path>> T visitFileTree(T visitor, URI uri) throws IOException
Files.walkFileTree(Path,FileVisitor)
and returns the given visitor.
Note that Files.walkFileTree(Path,FileVisitor)
returns the given path.T
- See Files.walkFileTree(Path,FileVisitor)
.visitor
- See Files.walkFileTree(Path,FileVisitor)
.uri
- See Paths.get(URI)
.IOException
- if an I/O error is thrown by a visitor methodpublic static Stream<Path> walk(Path start, PathFilter pathFilter, int maxDepth, boolean readAttributes, FileVisitOption... options) throws IOException
start
- the start pathpathFilter
- the path filtermaxDepth
- 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.IOException
- if an I/O error is thrown when accessing the starting file.Copyright © 2002–2021 The Apache Software Foundation. All rights reserved.