public final class PathUtils
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
static java.nio.file.CopyOption[] |
EMPTY_COPY_OPTIONS
Empty
CopyOption array. |
static DeleteOption[] |
EMPTY_DELETE_OPTION_ARRAY
Empty
DeleteOption array. |
static java.nio.file.attribute.FileAttribute<?>[] |
EMPTY_FILE_ATTRIBUTE_ARRAY
Empty
FileAttribute array. |
static java.nio.file.FileVisitOption[] |
EMPTY_FILE_VISIT_OPTION_ARRAY
Empty
FileVisitOption array. |
static java.nio.file.LinkOption[] |
EMPTY_LINK_OPTION_ARRAY
Empty
LinkOption array. |
static java.nio.file.OpenOption[] |
EMPTY_OPEN_OPTION_ARRAY
Empty
OpenOption array. |
static java.nio.file.Path[] |
EMPTY_PATH_ARRAY
Empty
Path array. |
static java.nio.file.LinkOption[] |
NOFOLLOW_LINK_OPTION_ARRAY
Deprecated.
|
Modifier and Type | Method and Description |
---|---|
static Counters.PathCounters |
cleanDirectory(java.nio.file.Path directory)
Cleans a directory including subdirectories without deleting directories.
|
static Counters.PathCounters |
cleanDirectory(java.nio.file.Path directory,
DeleteOption... deleteOptions)
Cleans a directory including subdirectories without deleting directories.
|
static long |
copy(IOSupplier<java.io.InputStream> in,
java.nio.file.Path target,
java.nio.file.CopyOption... copyOptions)
Copies the InputStream from the supplier with
Files.copy(InputStream, Path, CopyOption...) . |
static Counters.PathCounters |
copyDirectory(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.Path |
copyFile(java.net.URL sourceFile,
java.nio.file.Path targetFile,
java.nio.file.CopyOption... copyOptions)
Copies a URL to a directory.
|
static java.nio.file.Path |
copyFileToDirectory(java.nio.file.Path sourceFile,
java.nio.file.Path targetDirectory,
java.nio.file.CopyOption... copyOptions)
Copies a file to a directory.
|
static java.nio.file.Path |
copyFileToDirectory(java.net.URL sourceFile,
java.nio.file.Path targetDirectory,
java.nio.file.CopyOption... copyOptions)
Copies a URL to a directory.
|
static Counters.PathCounters |
countDirectory(java.nio.file.Path directory)
Counts aspects of a directory including subdirectories.
|
static Counters.PathCounters |
countDirectoryAsBigInteger(java.nio.file.Path directory)
Counts aspects of a directory including subdirectories.
|
static java.nio.file.Path |
createParentDirectories(java.nio.file.Path path,
java.nio.file.attribute.FileAttribute<?>... attrs)
Creates the parent directories for the given
path . |
static java.nio.file.Path |
createParentDirectories(java.nio.file.Path path,
java.nio.file.LinkOption linkOption,
java.nio.file.attribute.FileAttribute<?>... attrs)
Creates the parent directories for the given
path . |
static java.nio.file.Path |
current()
Gets the current directory.
|
static Counters.PathCounters |
delete(java.nio.file.Path path)
Deletes a file or directory.
|
static Counters.PathCounters |
delete(java.nio.file.Path path,
DeleteOption... deleteOptions)
Deletes a file or directory.
|
static Counters.PathCounters |
delete(java.nio.file.Path path,
java.nio.file.LinkOption[] linkOptions,
DeleteOption... deleteOptions)
Deletes a file or directory.
|
static Counters.PathCounters |
deleteDirectory(java.nio.file.Path directory)
Deletes a directory including subdirectories.
|
static Counters.PathCounters |
deleteDirectory(java.nio.file.Path directory,
DeleteOption... deleteOptions)
Deletes a directory including subdirectories.
|
static Counters.PathCounters |
deleteDirectory(java.nio.file.Path directory,
java.nio.file.LinkOption[] linkOptions,
DeleteOption... deleteOptions)
Deletes a directory including subdirectories.
|
static Counters.PathCounters |
deleteFile(java.nio.file.Path file)
Deletes the given file.
|
static Counters.PathCounters |
deleteFile(java.nio.file.Path file,
DeleteOption... deleteOptions)
Deletes the given file.
|
static Counters.PathCounters |
deleteFile(java.nio.file.Path file,
java.nio.file.LinkOption[] linkOptions,
DeleteOption... deleteOptions)
Deletes the given file.
|
static void |
deleteOnExit(java.nio.file.Path path)
Delegates to
File.deleteOnExit() . |
static boolean |
directoryAndFileContentEquals(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 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)
Compares the file sets of two Paths to determine if they are equal or not while considering file contents.
|
static boolean |
directoryContentEquals(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 boolean |
directoryContentEquals(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 boolean |
fileContentEquals(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 boolean |
fileContentEquals(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.nio.file.Path[] |
filter(PathFilter filter,
java.nio.file.Path... paths)
Applies an
IOFileFilter to the provided File objects. |
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 java.nio.file.attribute.AclFileAttributeView |
getAclFileAttributeView(java.nio.file.Path path,
java.nio.file.LinkOption... options)
Shorthand for
Files.getFileAttributeView(path, AclFileAttributeView.class) . |
static java.nio.file.attribute.DosFileAttributeView |
getDosFileAttributeView(java.nio.file.Path path,
java.nio.file.LinkOption... options)
Shorthand for
Files.getFileAttributeView(path, DosFileAttributeView.class) . |
static java.nio.file.attribute.FileTime |
getLastModifiedFileTime(java.io.File file)
Gets the file's last modified time or null if the file does not exist.
|
static java.nio.file.attribute.FileTime |
getLastModifiedFileTime(java.nio.file.Path path,
java.nio.file.attribute.FileTime defaultIfAbsent,
java.nio.file.LinkOption... options)
Gets the file's last modified time or null if the file does not exist.
|
static java.nio.file.attribute.FileTime |
getLastModifiedFileTime(java.nio.file.Path path,
java.nio.file.LinkOption... options)
Gets the file's last modified time or null if the file does not exist.
|
static java.nio.file.attribute.FileTime |
getLastModifiedFileTime(java.net.URI uri)
Gets the file's last modified time or null if the file does not exist.
|
static java.nio.file.attribute.FileTime |
getLastModifiedFileTime(java.net.URL url)
Gets the file's last modified time or null if the file does not exist.
|
static java.nio.file.attribute.PosixFileAttributeView |
getPosixFileAttributeView(java.nio.file.Path path,
java.nio.file.LinkOption... options)
Shorthand for
Files.getFileAttributeView(path, PosixFileAttributeView.class) . |
static java.nio.file.Path |
getTempDirectory()
Gets a
Path representing the system temporary directory. |
static boolean |
isDirectory(java.nio.file.Path path,
java.nio.file.LinkOption... options)
Tests whether the given
Path is a directory or not. |
static boolean |
isEmpty(java.nio.file.Path path)
Tests whether the given file or directory is empty.
|
static boolean |
isEmptyDirectory(java.nio.file.Path directory)
Tests whether the directory is empty.
|
static boolean |
isEmptyFile(java.nio.file.Path file)
Tests whether the given file is empty.
|
static boolean |
isNewer(java.nio.file.Path file,
java.time.chrono.ChronoZonedDateTime<?> czdt,
java.nio.file.LinkOption... options)
Tests if the given
Path is newer than the given time reference. |
static boolean |
isNewer(java.nio.file.Path file,
java.nio.file.attribute.FileTime fileTime,
java.nio.file.LinkOption... options)
Tests if the given
Path is newer than the given time reference. |
static boolean |
isNewer(java.nio.file.Path file,
java.time.Instant instant,
java.nio.file.LinkOption... options)
Tests if the given
Path is newer than the given time reference. |
static boolean |
isNewer(java.nio.file.Path file,
long timeMillis,
java.nio.file.LinkOption... options)
Tests if the given
Path is newer than the given time reference. |
static boolean |
isNewer(java.nio.file.Path file,
java.nio.file.Path reference)
Tests if the given
Path is newer than the reference Path . |
static boolean |
isOlder(java.nio.file.Path file,
java.nio.file.attribute.FileTime fileTime,
java.nio.file.LinkOption... options)
Tests if the given
Path is older than the given time reference. |
static boolean |
isOlder(java.nio.file.Path file,
java.time.Instant instant,
java.nio.file.LinkOption... options)
Tests if the given
Path is older than the given time reference. |
static boolean |
isOlder(java.nio.file.Path file,
long timeMillis,
java.nio.file.LinkOption... options)
Tests if the given
Path is older than the given time reference. |
static boolean |
isOlder(java.nio.file.Path file,
java.nio.file.Path reference)
Tests if the given
Path is older than the reference Path . |
static boolean |
isPosix(java.nio.file.Path test,
java.nio.file.LinkOption... options)
Tests whether the given path is on a POSIX file system.
|
static boolean |
isRegularFile(java.nio.file.Path path,
java.nio.file.LinkOption... options)
Tests whether the given
Path is a regular file or not. |
static java.nio.file.DirectoryStream<java.nio.file.Path> |
newDirectoryStream(java.nio.file.Path dir,
PathFilter pathFilter)
Creates a new DirectoryStream for Paths rooted at the given directory.
|
static java.io.OutputStream |
newOutputStream(java.nio.file.Path path,
boolean append)
Creates a new OutputStream by opening or creating a file, returning an output stream that may be used to write bytes
to the file.
|
static java.nio.file.LinkOption[] |
noFollowLinkOptionArray()
Copy of the
LinkOption array for LinkOption.NOFOLLOW_LINKS . |
static <A extends java.nio.file.attribute.BasicFileAttributes> |
readAttributes(java.nio.file.Path path,
java.lang.Class<A> type,
java.nio.file.LinkOption... options)
Reads the BasicFileAttributes from the given path.
|
static java.nio.file.attribute.BasicFileAttributes |
readBasicFileAttributes(java.nio.file.Path path)
Deprecated.
Will be removed in 3.0.0 in favor of
readBasicFileAttributes(Path, LinkOption...) . |
static java.nio.file.attribute.BasicFileAttributes |
readBasicFileAttributes(java.nio.file.Path path,
java.nio.file.LinkOption... options)
Reads the BasicFileAttributes from the given path.
|
static java.nio.file.attribute.BasicFileAttributes |
readBasicFileAttributesUnchecked(java.nio.file.Path path)
Deprecated.
|
static java.nio.file.attribute.DosFileAttributes |
readDosFileAttributes(java.nio.file.Path path,
java.nio.file.LinkOption... options)
Reads the DosFileAttributes from the given path.
|
static java.nio.file.attribute.BasicFileAttributes |
readOsFileAttributes(java.nio.file.Path path,
java.nio.file.LinkOption... options)
Reads the PosixFileAttributes or DosFileAttributes from the given path.
|
static java.nio.file.attribute.PosixFileAttributes |
readPosixFileAttributes(java.nio.file.Path path,
java.nio.file.LinkOption... options)
Reads the PosixFileAttributes from the given path.
|
static java.lang.String |
readString(java.nio.file.Path path,
java.nio.charset.Charset charset)
Reads the given path as a String.
|
static void |
setLastModifiedTime(java.nio.file.Path sourceFile,
java.nio.file.Path targetFile)
Sets the given
targetFile 's last modified time to the value from sourceFile . |
static java.nio.file.Path |
setReadOnly(java.nio.file.Path path,
boolean readOnly,
java.nio.file.LinkOption... linkOptions)
Sets the given Path to the
readOnly value. |
static long |
sizeOf(java.nio.file.Path path)
Returns the size of the given file or directory.
|
static java.math.BigInteger |
sizeOfAsBigInteger(java.nio.file.Path path)
Returns the size of the given file or directory.
|
static long |
sizeOfDirectory(java.nio.file.Path directory)
Counts the size of a directory recursively (sum of the size of all files).
|
static java.math.BigInteger |
sizeOfDirectoryAsBigInteger(java.nio.file.Path directory)
Counts the size of a directory recursively (sum of the size of all files).
|
static java.nio.file.Path |
touch(java.nio.file.Path file)
Implements behavior similar to the Unix "touch" utility.
|
static <T extends java.nio.file.FileVisitor<? super java.nio.file.Path>> |
visitFileTree(T visitor,
java.nio.file.Path directory)
Performs
Files.walkFileTree(Path,FileVisitor) and returns the given visitor. |
static <T extends java.nio.file.FileVisitor<? super java.nio.file.Path>> |
visitFileTree(T visitor,
java.nio.file.Path start,
java.util.Set<java.nio.file.FileVisitOption> options,
int maxDepth)
Performs
Files.walkFileTree(Path,FileVisitor) and returns the given visitor. |
static <T extends java.nio.file.FileVisitor<? super java.nio.file.Path>> |
visitFileTree(T visitor,
java.lang.String first,
java.lang.String... more)
Performs
Files.walkFileTree(Path,FileVisitor) and returns the given visitor. |
static <T extends java.nio.file.FileVisitor<? super java.nio.file.Path>> |
visitFileTree(T visitor,
java.net.URI uri)
Performs
Files.walkFileTree(Path,FileVisitor) and returns the given visitor. |
static boolean |
waitFor(java.nio.file.Path file,
java.time.Duration timeout,
java.nio.file.LinkOption... options)
Waits for the file system to propagate a file creation, with a timeout.
|
static java.util.stream.Stream<java.nio.file.Path> |
walk(java.nio.file.Path start,
PathFilter pathFilter,
int maxDepth,
boolean readAttributes,
java.nio.file.FileVisitOption... options)
Returns a stream of filtered paths.
|
static java.nio.file.Path |
writeString(java.nio.file.Path path,
java.lang.CharSequence charSequence,
java.nio.charset.Charset charset,
java.nio.file.OpenOption... openOptions)
Writes the given character sequence to a file at the given path.
|
public static final java.nio.file.CopyOption[] EMPTY_COPY_OPTIONS
CopyOption
array.public static final DeleteOption[] EMPTY_DELETE_OPTION_ARRAY
DeleteOption
array.public static final java.nio.file.attribute.FileAttribute<?>[] EMPTY_FILE_ATTRIBUTE_ARRAY
FileAttribute
array.public static final java.nio.file.FileVisitOption[] EMPTY_FILE_VISIT_OPTION_ARRAY
FileVisitOption
array.public static final java.nio.file.LinkOption[] EMPTY_LINK_OPTION_ARRAY
LinkOption
array.@Deprecated public static final java.nio.file.LinkOption[] NOFOLLOW_LINK_OPTION_ARRAY
noFollowLinkOptionArray()
.LinkOption
array for LinkOption.NOFOLLOW_LINKS
.public static final java.nio.file.OpenOption[] EMPTY_OPEN_OPTION_ARRAY
OpenOption
array.public static final java.nio.file.Path[] EMPTY_PATH_ARRAY
Path
array.public static Counters.PathCounters cleanDirectory(java.nio.file.Path directory) throws java.io.IOException
directory
- directory to clean.java.io.IOException
- if an I/O error is thrown by a visitor method.public static Counters.PathCounters cleanDirectory(java.nio.file.Path directory, DeleteOption... deleteOptions) throws java.io.IOException
directory
- directory to clean.deleteOptions
- How to handle deletion.java.io.IOException
- if an I/O error is thrown by a visitor method.public static long copy(IOSupplier<java.io.InputStream> in, java.nio.file.Path target, java.nio.file.CopyOption... copyOptions) throws java.io.IOException
Files.copy(InputStream, Path, CopyOption...)
.in
- Supplies the InputStream.target
- See Files.copy(InputStream, Path, CopyOption...)
.copyOptions
- See Files.copy(InputStream, Path, CopyOption...)
.Files.copy(InputStream, Path, CopyOption...)
java.io.IOException
- See Files.copy(InputStream, Path, CopyOption...)
public static Counters.PathCounters copyDirectory(java.nio.file.Path sourceDirectory, java.nio.file.Path targetDirectory, java.nio.file.CopyOption... copyOptions) throws java.io.IOException
sourceDirectory
- The source directory.targetDirectory
- The target directory.copyOptions
- Specifies how the copying should be done.java.io.IOException
- if an I/O error is thrown by a visitor method.public static java.nio.file.Path copyFile(java.net.URL sourceFile, java.nio.file.Path targetFile, java.nio.file.CopyOption... copyOptions) throws java.io.IOException
sourceFile
- The source URL.targetFile
- The target file.copyOptions
- Specifies how the copying should be done.java.io.IOException
- if an I/O error occurs.Files.copy(InputStream, Path, CopyOption...)
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.IOException
sourceFile
- The source file.targetDirectory
- The target directory.copyOptions
- Specifies how the copying should be done.java.io.IOException
- if an I/O error occurs.Files.copy(Path, Path, CopyOption...)
public static java.nio.file.Path copyFileToDirectory(java.net.URL sourceFile, java.nio.file.Path targetDirectory, java.nio.file.CopyOption... copyOptions) throws java.io.IOException
sourceFile
- The source URL.targetDirectory
- The target directory.copyOptions
- Specifies how the copying should be done.java.io.IOException
- if an I/O error occurs.Files.copy(InputStream, Path, CopyOption...)
public static Counters.PathCounters countDirectory(java.nio.file.Path directory) throws java.io.IOException
directory
- directory to delete.java.io.IOException
- if an I/O error is thrown by a visitor method.public static Counters.PathCounters countDirectoryAsBigInteger(java.nio.file.Path directory) throws java.io.IOException
directory
- directory to count.java.io.IOException
- if an I/O error occurs.public static java.nio.file.Path createParentDirectories(java.nio.file.Path path, java.nio.file.attribute.FileAttribute<?>... attrs) throws java.io.IOException
path
.
If the parent directory already exists, then return it.
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.java.io.IOException
- if an I/O error occurs.public static java.nio.file.Path createParentDirectories(java.nio.file.Path path, java.nio.file.LinkOption linkOption, java.nio.file.attribute.FileAttribute<?>... attrs) throws java.io.IOException
path
.
If the parent directory already exists, then return it.
path
- The path to a file (or directory).linkOption
- A LinkOption
or null.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.java.io.IOException
- if an I/O error occurs.public static java.nio.file.Path current()
public static Counters.PathCounters delete(java.nio.file.Path path) throws java.io.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
java.lang.NullPointerException
- if the directory is null
java.io.IOException
- if an I/O error is thrown by a visitor method or if an I/O error occurs.public static Counters.PathCounters delete(java.nio.file.Path path, DeleteOption... deleteOptions) throws java.io.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.java.lang.NullPointerException
- if the directory is null
java.io.IOException
- if an I/O error is thrown by a visitor method or if an I/O error occurs.public static Counters.PathCounters delete(java.nio.file.Path path, java.nio.file.LinkOption[] linkOptions, DeleteOption... deleteOptions) throws java.io.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.java.lang.NullPointerException
- if the directory is null
java.io.IOException
- if an I/O error is thrown by a visitor method or if an I/O error occurs.public static Counters.PathCounters deleteDirectory(java.nio.file.Path directory) throws java.io.IOException
directory
- directory to delete.java.io.IOException
- if an I/O error is thrown by a visitor method.public static Counters.PathCounters deleteDirectory(java.nio.file.Path directory, DeleteOption... deleteOptions) throws java.io.IOException
directory
- directory to delete.deleteOptions
- How to handle deletion.java.io.IOException
- if an I/O error is thrown by a visitor method.public static Counters.PathCounters deleteDirectory(java.nio.file.Path directory, java.nio.file.LinkOption[] linkOptions, DeleteOption... deleteOptions) throws java.io.IOException
directory
- directory to delete.linkOptions
- How to handle symbolic links.deleteOptions
- How to handle deletion.java.io.IOException
- if an I/O error is thrown by a visitor method.public static Counters.PathCounters deleteFile(java.nio.file.Path file) throws java.io.IOException
file
- The file to delete.java.io.IOException
- if an I/O error occurs.java.nio.file.NoSuchFileException
- if the file is a directory.public static Counters.PathCounters deleteFile(java.nio.file.Path file, DeleteOption... deleteOptions) throws java.io.IOException
file
- The file to delete.deleteOptions
- How to handle deletion.java.io.IOException
- if an I/O error occurs.java.nio.file.NoSuchFileException
- if the file is a directory.public static Counters.PathCounters deleteFile(java.nio.file.Path file, java.nio.file.LinkOption[] linkOptions, DeleteOption... deleteOptions) throws java.nio.file.NoSuchFileException, java.io.IOException
file
- The file to delete.linkOptions
- How to handle symbolic links.deleteOptions
- How to handle deletion.java.io.IOException
- if an I/O error occurs.java.nio.file.NoSuchFileException
- if the file is a directory.public static void deleteOnExit(java.nio.file.Path path)
File.deleteOnExit()
.path
- the path to delete.public static boolean directoryAndFileContentEquals(java.nio.file.Path path1, java.nio.file.Path path2) throws java.io.IOException
path1
- The first directory.path2
- The second directory.java.io.IOException
- if an I/O error is thrown by a visitor method.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.IOException
path1
- The first directory.path2
- The second directory.linkOptions
- options to follow links.openOptions
- options to open files.fileVisitOption
- options to configure traversal.java.io.IOException
- if an I/O error is thrown by a visitor method.public static boolean directoryContentEquals(java.nio.file.Path path1, java.nio.file.Path path2) throws java.io.IOException
path1
- The first directory.path2
- The second directory.java.io.IOException
- if an I/O error is thrown by a visitor method.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.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 traversaljava.io.IOException
- if an I/O error is thrown by a visitor method.public static boolean fileContentEquals(java.nio.file.Path path1, java.nio.file.Path path2) throws java.io.IOException
File content is accessed through Files.newInputStream(Path,OpenOption...)
.
path1
- the first stream.path2
- the second stream.java.lang.NullPointerException
- if either input is null.java.io.IOException
- if an I/O error occurs.FileUtils.contentEquals(java.io.File, java.io.File)
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.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.java.lang.NullPointerException
- if openOptions is null.java.io.IOException
- if an I/O error occurs.FileUtils.contentEquals(java.io.File, java.io.File)
public static java.nio.file.Path[] filter(PathFilter filter, java.nio.file.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.java.lang.NullPointerException
- if the filter is null
java.lang.IllegalArgumentException
- if files
contains a null
value.public static java.util.List<java.nio.file.attribute.AclEntry> getAclEntryList(java.nio.file.Path sourcePath) throws java.io.IOException
sourcePath
- the path to the file.java.io.IOException
- if an I/O error occurs.public static java.nio.file.attribute.AclFileAttributeView getAclFileAttributeView(java.nio.file.Path path, java.nio.file.LinkOption... options)
Files.getFileAttributeView(path, AclFileAttributeView.class)
.path
- the path to the file.options
- how to handle symbolic links.null
if the attribute view type is not available.public static java.nio.file.attribute.DosFileAttributeView getDosFileAttributeView(java.nio.file.Path path, java.nio.file.LinkOption... options)
Files.getFileAttributeView(path, DosFileAttributeView.class)
.path
- the path to the file.options
- how to handle symbolic links.null
if the attribute view type is not available.public static java.nio.file.attribute.FileTime getLastModifiedFileTime(java.io.File file) throws java.io.IOException
The method provides a workaround for bug JDK-8177809
where File.lastModified()
looses milliseconds and always ends in 000. This bug is in OpenJDK 8 and 9, and
fixed in 11.
file
- the file to query.java.io.IOException
- Thrown if an I/O error occurs.public static java.nio.file.attribute.FileTime getLastModifiedFileTime(java.nio.file.Path path, java.nio.file.attribute.FileTime defaultIfAbsent, java.nio.file.LinkOption... options) throws java.io.IOException
path
- the file to query.defaultIfAbsent
- Returns this file time of the file does not exist, may be null.options
- options indicating how symbolic links are handled.java.io.IOException
- Thrown if an I/O error occurs.public static java.nio.file.attribute.FileTime getLastModifiedFileTime(java.nio.file.Path path, java.nio.file.LinkOption... options) throws java.io.IOException
path
- the file to query.options
- options indicating how symbolic links are handled.java.io.IOException
- Thrown if an I/O error occurs.public static java.nio.file.attribute.FileTime getLastModifiedFileTime(java.net.URI uri) throws java.io.IOException
uri
- the file to query.java.io.IOException
- Thrown if an I/O error occurs.public static java.nio.file.attribute.FileTime getLastModifiedFileTime(java.net.URL url) throws java.io.IOException, java.net.URISyntaxException
url
- the file to query.java.io.IOException
- Thrown if an I/O error occurs.java.net.URISyntaxException
- if the URL is not formatted strictly according to RFC2396 and cannot be converted to a
URI.public static java.nio.file.attribute.PosixFileAttributeView getPosixFileAttributeView(java.nio.file.Path path, java.nio.file.LinkOption... options)
Files.getFileAttributeView(path, PosixFileAttributeView.class)
.path
- the path to the file.options
- how to handle symbolic links.null
if the attribute view type is not available.public static java.nio.file.Path getTempDirectory()
Path
representing the system temporary directory.public static boolean isDirectory(java.nio.file.Path path, java.nio.file.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 to handle symbolic linkstrue
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.java.lang.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(java.nio.file.Path path) throws java.io.IOException
path
- the file or directory to query.java.io.IOException
- if an I/O error occurs.public static boolean isEmptyDirectory(java.nio.file.Path directory) throws java.io.IOException
directory
- the directory to query.java.nio.file.NotDirectoryException
- if the file could not otherwise be opened because it is not a directory (optional
specific exception).java.io.IOException
- if an I/O error occurs.java.lang.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(java.nio.file.Path file) throws java.io.IOException
file
- the file to query.java.io.IOException
- if an I/O error occurs.java.lang.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(java.nio.file.Path file, java.time.chrono.ChronoZonedDateTime<?> czdt, java.nio.file.LinkOption... options) throws java.io.IOException
Path
is newer than the given time reference.file
- the Path
to test.czdt
- the time reference.options
- options indicating how to handle symbolic links.Path
exists and has been modified after the given time reference.java.io.IOException
- if an I/O error occurs.java.lang.NullPointerException
- if the file is null
.public static boolean isNewer(java.nio.file.Path file, java.nio.file.attribute.FileTime fileTime, java.nio.file.LinkOption... options) throws java.io.IOException
Path
is newer than the given time reference.file
- the Path
to test.fileTime
- the time reference.options
- options indicating how to handle symbolic links.Path
exists and has been modified after the given time reference.java.io.IOException
- if an I/O error occurs.java.lang.NullPointerException
- if the file is null
.public static boolean isNewer(java.nio.file.Path file, java.time.Instant instant, java.nio.file.LinkOption... options) throws java.io.IOException
Path
is newer than the given time reference.file
- the Path
to test.instant
- the time reference.options
- options indicating how to handle symbolic links.Path
exists and has been modified after the given time reference.java.io.IOException
- if an I/O error occurs.java.lang.NullPointerException
- if the file is null
.public static boolean isNewer(java.nio.file.Path file, long timeMillis, java.nio.file.LinkOption... options) throws java.io.IOException
Path
is newer than the given time reference.file
- the Path
to test.timeMillis
- the time reference measured in milliseconds since the epoch (00:00:00 GMT, January 1, 1970)options
- options indicating how to handle symbolic links.Path
exists and has been modified after the given time reference.java.io.IOException
- if an I/O error occurs.java.lang.NullPointerException
- if the file is null
.public static boolean isNewer(java.nio.file.Path file, java.nio.file.Path reference) throws java.io.IOException
Path
is newer than the reference Path
.file
- the File
to test.reference
- the File
of which the modification date is used.File
exists and has been modified more recently than the reference File
.java.io.IOException
- if an I/O error occurs.public static boolean isOlder(java.nio.file.Path file, java.nio.file.attribute.FileTime fileTime, java.nio.file.LinkOption... options) throws java.io.IOException
Path
is older than the given time reference.file
- the Path
to test.fileTime
- the time reference.options
- options indicating how to handle symbolic links.Path
exists and has been modified before the given time reference.java.io.IOException
- if an I/O error occurs.java.lang.NullPointerException
- if the file is null
.public static boolean isOlder(java.nio.file.Path file, java.time.Instant instant, java.nio.file.LinkOption... options) throws java.io.IOException
Path
is older than the given time reference.file
- the Path
to test.instant
- the time reference.options
- options indicating how to handle symbolic links.Path
exists and has been modified before the given time reference.java.io.IOException
- if an I/O error occurs.java.lang.NullPointerException
- if the file is null
.public static boolean isOlder(java.nio.file.Path file, long timeMillis, java.nio.file.LinkOption... options) throws java.io.IOException
Path
is older than the given time reference.file
- the Path
to test.timeMillis
- the time reference measured in milliseconds since the epoch (00:00:00 GMT, January 1, 1970)options
- options indicating how to handle symbolic links.Path
exists and has been modified before the given time reference.java.io.IOException
- if an I/O error occurs.java.lang.NullPointerException
- if the file is null
.public static boolean isOlder(java.nio.file.Path file, java.nio.file.Path reference) throws java.io.IOException
Path
is older than the reference Path
.file
- the File
to test.reference
- the File
of which the modification date is used.File
exists and has been modified before than the reference File
.java.io.IOException
- if an I/O error occurs.public static boolean isPosix(java.nio.file.Path test, java.nio.file.LinkOption... options)
test
- The Path to test.options
- options indicating how to handle symbolic links.public static boolean isRegularFile(java.nio.file.Path path, java.nio.file.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 to handle symbolic links.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.java.lang.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 java.nio.file.DirectoryStream<java.nio.file.Path> newDirectoryStream(java.nio.file.Path dir, PathFilter pathFilter) throws java.io.IOException
dir
- the path to the directory to stream.pathFilter
- the directory stream filter.java.io.IOException
- if an I/O error occurs.public static java.io.OutputStream newOutputStream(java.nio.file.Path path, boolean append) throws java.io.IOException
path
- the Path.append
- Whether or not to append.java.io.IOException
- if an I/O error occurs.Files.newOutputStream(Path, OpenOption...)
public static java.nio.file.LinkOption[] noFollowLinkOptionArray()
LinkOption
array for LinkOption.NOFOLLOW_LINKS
.LinkOption
array for LinkOption.NOFOLLOW_LINKS
.public static <A extends java.nio.file.attribute.BasicFileAttributes> A readAttributes(java.nio.file.Path path, java.lang.Class<A> type, java.nio.file.LinkOption... options)
UnsupportedOperationException
. Throws Uncheck
instead of IOException
.A
- The BasicFileAttributes
typepath
- The Path to test.type
- the Class
of the file attributes required to read.options
- options indicating how to handle symbolic links.Files.readAttributes(Path, Class, LinkOption...)
@Deprecated public static java.nio.file.attribute.BasicFileAttributes readBasicFileAttributes(java.nio.file.Path path) throws java.io.IOException
readBasicFileAttributes(Path, LinkOption...)
.path
- the path to read.java.io.IOException
- if an I/O error occurs.public static java.nio.file.attribute.BasicFileAttributes readBasicFileAttributes(java.nio.file.Path path, java.nio.file.LinkOption... options)
UnsupportedOperationException
.path
- the path to read.options
- options indicating how to handle symbolic links.@Deprecated public static java.nio.file.attribute.BasicFileAttributes readBasicFileAttributesUnchecked(java.nio.file.Path path)
readBasicFileAttributes(Path, LinkOption...)
.UnsupportedOperationException
.path
- the path to read.java.io.UncheckedIOException
- if an I/O error occurspublic static java.nio.file.attribute.DosFileAttributes readDosFileAttributes(java.nio.file.Path path, java.nio.file.LinkOption... options)
UnsupportedOperationException
.path
- the path to read.options
- options indicating how to handle symbolic links.public static java.nio.file.attribute.BasicFileAttributes readOsFileAttributes(java.nio.file.Path path, java.nio.file.LinkOption... options)
UnsupportedOperationException
.path
- The Path to read.options
- options indicating how to handle symbolic links.public static java.nio.file.attribute.PosixFileAttributes readPosixFileAttributes(java.nio.file.Path path, java.nio.file.LinkOption... options)
UnsupportedOperationException
.path
- The Path to read.options
- options indicating how to handle symbolic links.public static java.lang.String readString(java.nio.file.Path path, java.nio.charset.Charset charset) throws java.io.IOException
path
- The source path.charset
- How to convert bytes to a String, null uses the default Charset.java.io.IOException
- if an I/O error occurs reading from the stream.Files.readAllBytes(Path)
public static void setLastModifiedTime(java.nio.file.Path sourceFile, java.nio.file.Path targetFile) throws java.io.IOException
targetFile
's last modified time to the value from sourceFile
.sourceFile
- The source path to query.targetFile
- The target path to set.java.lang.NullPointerException
- if sourceFile is null
.java.lang.NullPointerException
- if targetFile is null
.java.io.IOException
- if setting the last-modified time failed.public static java.nio.file.Path setReadOnly(java.nio.file.Path path, boolean readOnly, java.nio.file.LinkOption... linkOptions) throws java.io.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 to handle symbolic links.java.io.IOException
- if an I/O error occurs.public static long sizeOf(java.nio.file.Path path) throws java.io.IOException
Path
is a regular file, then the file's size
is returned. If the argument is a directory, then the size of the directory is calculated recursively.
Note that overflow is not detected, and the return value may be negative if overflow occurs. See
sizeOfAsBigInteger(Path)
for an alternative method that does not overflow.
path
- the regular file or directory to return the size of, must not be null
.java.lang.NullPointerException
- if the file is null
.java.lang.IllegalArgumentException
- if the file does not exist.java.io.IOException
- if an I/O error occurs.public static java.math.BigInteger sizeOfAsBigInteger(java.nio.file.Path path) throws java.io.IOException
Path
is a regular file, then the file's size
is returned. If the argument is a directory, then the size of the directory is calculated recursively.path
- the regular file or directory to return the size of (must not be null
).java.lang.NullPointerException
- if the file is null
.java.lang.IllegalArgumentException
- if the file does not exist.java.io.IOException
- if an I/O error occurs.public static long sizeOfDirectory(java.nio.file.Path directory) throws java.io.IOException
Note that overflow is not detected, and the return value may be negative if overflow occurs. See
sizeOfDirectoryAsBigInteger(Path)
for an alternative method that does not overflow.
directory
- directory to inspect, must not be null
.Long.MAX_VALUE
.java.lang.NullPointerException
- if the directory is null
.java.io.IOException
- if an I/O error occurs.public static java.math.BigInteger sizeOfDirectoryAsBigInteger(java.nio.file.Path directory) throws java.io.IOException
directory
- directory to inspect, must not be null
.java.lang.NullPointerException
- if the directory is null
.java.io.IOException
- if an I/O error occurs.public static java.nio.file.Path touch(java.nio.file.Path file) throws java.io.IOException
file
- the file to touch.java.lang.NullPointerException
- if the parameter is null
.java.io.IOException
- if setting the last-modified time failed or an I/O problem occurs.\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.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)
.java.io.IOException
- if an I/O error is thrown by a visitor method.java.lang.NullPointerException
- if the directory is null
.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.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)
.java.io.IOException
- if an I/O error is thrown by a visitor method.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.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[])
.java.io.IOException
- if an I/O error is thrown by a visitor method.public static <T extends java.nio.file.FileVisitor<? super java.nio.file.Path>> T visitFileTree(T visitor, java.net.URI uri) throws java.io.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)
.java.io.IOException
- if an I/O error is thrown by a visitor method.public static boolean waitFor(java.nio.file.Path file, java.time.Duration timeout, java.nio.file.LinkOption... options)
This method repeatedly tests Files.exists(Path,LinkOption...)
until it returns true up to the maximum time
given.
file
- the file to check, must not be null
.timeout
- the maximum time to wait.options
- options indicating how to handle symbolic links.java.lang.NullPointerException
- if the file is null
.public static java.util.stream.Stream<java.nio.file.Path> walk(java.nio.file.Path start, PathFilter pathFilter, int maxDepth, boolean readAttributes, java.nio.file.FileVisitOption... options) throws java.io.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.java.io.IOException
- if an I/O error is thrown when accessing the starting file.public static java.nio.file.Path writeString(java.nio.file.Path path, java.lang.CharSequence charSequence, java.nio.charset.Charset charset, java.nio.file.OpenOption... openOptions) throws java.io.IOException
path
- The target file.charSequence
- The character sequence text.charset
- The Charset to encode the text.openOptions
- options How to open the file.java.io.IOException
- if an I/O error occurs writing to or creating the file.java.lang.NullPointerException
- if either path
or charSequence
is null
.Copyright © 2010 - 2023 Adobe. All Rights Reserved