Class PathUtils
- Since:
- 2.7
-
Field Summary
Modifier and TypeFieldDescriptionstatic final CopyOption[]
EmptyCopyOption
array.static final DeleteOption[]
EmptyDeleteOption
array.static final FileAttribute<?>[]
EmptyFileAttribute
array.static final FileVisitOption[]
EmptyFileVisitOption
array.static final LinkOption[]
EmptyLinkOption
array.static final OpenOption[]
EmptyOpenOption
array.static final Path[]
EmptyPath
array.static final LinkOption[]
Deprecated. -
Method Summary
Modifier and TypeMethodDescriptionstatic Counters.PathCounters
cleanDirectory
(Path directory) Cleans a directory by deleting only files, including in subdirectories, but without deleting the directories.static Counters.PathCounters
cleanDirectory
(Path directory, DeleteOption... deleteOptions) Cleans a directory by deleting only files, including in subdirectories, but without deleting the directories.static long
copy
(IOSupplier<InputStream> in, Path target, CopyOption... copyOptions) Copies the InputStream from the supplier withFiles.copy(InputStream, Path, CopyOption...)
.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
(URL sourceFile, Path targetDirectory, CopyOption... copyOptions) Copies a URL to a directory.static Path
copyFileToDirectory
(Path sourceFile, Path targetDirectory, CopyOption... copyOptions) Copies a file to a directory.static Counters.PathCounters
countDirectory
(Path directory) Counts aspects of a directory including subdirectories.static Counters.PathCounters
countDirectoryAsBigInteger
(Path directory) Counts aspects of a directory including subdirectories.static Path
createParentDirectories
(Path path, FileAttribute<?>... attrs) Creates the parent directories for the givenpath
.static Path
createParentDirectories
(Path path, LinkOption linkOption, FileAttribute<?>... attrs) Creates the parent directories for the givenpath
.static Path
current()
Gets the current directory.static Counters.PathCounters
Deletes a file or directory.static Counters.PathCounters
delete
(Path path, LinkOption[] linkOptions, DeleteOption... deleteOptions) Deletes a file or directory.static Counters.PathCounters
delete
(Path path, DeleteOption... deleteOptions) Deletes a file or directory.static Counters.PathCounters
deleteDirectory
(Path directory) Deletes a directory including subdirectories.static Counters.PathCounters
deleteDirectory
(Path directory, LinkOption[] linkOptions, DeleteOption... deleteOptions) Deletes a directory including subdirectories.static Counters.PathCounters
deleteDirectory
(Path directory, DeleteOption... deleteOptions) Deletes a directory including subdirectories.static Counters.PathCounters
deleteFile
(Path file) Deletes the given file.static Counters.PathCounters
deleteFile
(Path file, LinkOption[] linkOptions, DeleteOption... deleteOptions) Deletes the given file.static Counters.PathCounters
deleteFile
(Path file, DeleteOption... deleteOptions) Deletes the given file.static void
deleteOnExit
(Path path) Delegates toFile.deleteOnExit()
.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 anIOFileFilter
to the providedFile
objects.getAclEntryList
(Path sourcePath) Reads the access control list from a file attribute view.static AclFileAttributeView
getAclFileAttributeView
(Path path, LinkOption... options) Shorthand forFiles.getFileAttributeView(path, AclFileAttributeView.class)
.static String
getBaseName
(Path path) Gets the base name (the part up to and not including the last ".") of the last path segment of a file name.static DosFileAttributeView
getDosFileAttributeView
(Path path, LinkOption... options) Shorthand forFiles.getFileAttributeView(path, DosFileAttributeView.class, options)
.static String
getExtension
(Path path) Gets the extension of a Path.static <R> R
getFileName
(Path path, Function<Path, R> function) Gets the Path's file name and apply the given function if the file name is non-null.static String
getFileNameString
(Path path) Gets the Path's file name as a string.static FileTime
getLastModifiedFileTime
(File file) Gets the file's last modified time or null if the file does not exist.static FileTime
Gets the file's last modified time or null if the file does not exist.static FileTime
Gets the file's last modified time or null if the file does not exist.static FileTime
getLastModifiedFileTime
(Path path, FileTime defaultIfAbsent, LinkOption... options) Gets the file's last modified time or null if the file does not exist.static FileTime
getLastModifiedFileTime
(Path path, LinkOption... options) Gets the file's last modified time or null if the file does not exist.static PosixFileAttributeView
getPosixFileAttributeView
(Path path, LinkOption... options) Shorthand forFiles.getFileAttributeView(path, PosixFileAttributeView.class)
.static Path
Gets aPath
representing the system temporary directory.static boolean
isDirectory
(Path path, LinkOption... options) Tests whether the givenPath
is a directory or not.static boolean
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 givenPath
is newer than the given time reference.static boolean
isNewer
(Path file, FileTime fileTime, LinkOption... options) Tests if the givenPath
is newer than the given time reference.static boolean
static boolean
isNewer
(Path file, ChronoZonedDateTime<?> czdt, LinkOption... options) Tests if the givenPath
is newer than the given time reference.static boolean
isNewer
(Path file, Instant instant, LinkOption... options) Tests if the givenPath
is newer than the given time reference.static boolean
isOlder
(Path file, long timeMillis, LinkOption... options) Tests if the givenPath
is older than the given time reference.static boolean
isOlder
(Path file, FileTime fileTime, LinkOption... options) Tests if the givenPath
is older than the given time reference.static boolean
static boolean
isOlder
(Path file, Instant instant, LinkOption... options) Tests if the givenPath
is older than the given time reference.static boolean
isPosix
(Path test, LinkOption... options) Tests whether the given path is on a POSIX file system.static boolean
isRegularFile
(Path path, LinkOption... options) Tests whether the givenPath
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 OutputStream
newOutputStream
(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 LinkOption[]
Copy of theLinkOption
array forLinkOption.NOFOLLOW_LINKS
.static <A extends BasicFileAttributes>
AreadAttributes
(Path path, Class<A> type, LinkOption... options) Reads the BasicFileAttributes from the given path.static BasicFileAttributes
readBasicFileAttributes
(Path path) Reads the BasicFileAttributes from the given path.static BasicFileAttributes
readBasicFileAttributes
(Path path, LinkOption... options) Reads the BasicFileAttributes from the given path.static BasicFileAttributes
Deprecated.static DosFileAttributes
readDosFileAttributes
(Path path, LinkOption... options) Reads the DosFileAttributes from the given path.static BasicFileAttributes
readOsFileAttributes
(Path path, LinkOption... options) Reads the PosixFileAttributes or DosFileAttributes from the given path.static PosixFileAttributes
readPosixFileAttributes
(Path path, LinkOption... options) Reads the PosixFileAttributes from the given path.static String
readString
(Path path, Charset charset) Reads the file contents at the given path as a String using the Charset.static void
setLastModifiedTime
(Path sourceFile, Path targetFile) Sets the giventargetFile
's last modified time to the value fromsourceFile
.static Path
setReadOnly
(Path path, boolean readOnly, LinkOption... linkOptions) Sets the given Path to thereadOnly
value.static long
Returns the size of the given file or directory.static BigInteger
sizeOfAsBigInteger
(Path path) Returns the size of the given file or directory.static long
sizeOfDirectory
(Path directory) Counts the size of a directory recursively (sum of the size of all files).static BigInteger
sizeOfDirectoryAsBigInteger
(Path directory) Counts the size of a directory recursively (sum of the size of all files).static Path
Implements behavior similar to the UNIX "touch" utility.static <T extends FileVisitor<? super Path>>
TvisitFileTree
(T visitor, String first, String... more) PerformsFiles.walkFileTree(Path,FileVisitor)
and returns the given visitor.static <T extends FileVisitor<? super Path>>
TvisitFileTree
(T visitor, URI uri) PerformsFiles.walkFileTree(Path,FileVisitor)
and returns the given visitor.static <T extends FileVisitor<? super Path>>
TvisitFileTree
(T visitor, Path directory) PerformsFiles.walkFileTree(Path,FileVisitor)
and returns the given visitor.static <T extends FileVisitor<? super Path>>
TvisitFileTree
(T visitor, Path start, Set<FileVisitOption> options, int maxDepth) PerformsFiles.walkFileTree(Path,FileVisitor)
and returns the given visitor.static boolean
waitFor
(Path file, Duration timeout, LinkOption... options) Waits for the file system to detect a file's presence, with a timeout.walk
(Path start, PathFilter pathFilter, int maxDepth, boolean readAttributes, FileVisitOption... options) Returns a stream of filtered paths.static Path
writeString
(Path path, CharSequence charSequence, Charset charset, OpenOption... openOptions) Writes the given character sequence to a file at the given path.
-
Field Details
-
EMPTY_COPY_OPTIONS
EmptyCopyOption
array.- Since:
- 2.8.0
-
EMPTY_DELETE_OPTION_ARRAY
EmptyDeleteOption
array.- Since:
- 2.8.0
-
EMPTY_FILE_ATTRIBUTE_ARRAY
EmptyFileAttribute
array.- Since:
- 2.13.0
-
EMPTY_FILE_VISIT_OPTION_ARRAY
EmptyFileVisitOption
array. -
EMPTY_LINK_OPTION_ARRAY
EmptyLinkOption
array. -
NOFOLLOW_LINK_OPTION_ARRAY
Deprecated.LinkOption
array forLinkOption.NOFOLLOW_LINKS
.- Since:
- 2.9.0
-
EMPTY_OPEN_OPTION_ARRAY
EmptyOpenOption
array. -
EMPTY_PATH_ARRAY
EmptyPath
array.- Since:
- 2.9.0
-
-
Method Details
-
cleanDirectory
Cleans a directory by deleting only files, including in subdirectories, but without deleting the directories.- Parameters:
directory
- directory to clean.- Returns:
- The visitation path counters.
- Throws:
IOException
- if an I/O error is thrown by a visitor method.
-
cleanDirectory
public static Counters.PathCounters cleanDirectory(Path directory, DeleteOption... deleteOptions) throws IOException Cleans a directory by deleting only files, including in subdirectories, but without deleting the directories.- Parameters:
directory
- directory to clean.deleteOptions
- How to handle deletion.- Returns:
- The visitation path counters.
- Throws:
IOException
- if an I/O error is thrown by a visitor method.- Since:
- 2.8.0
-
copy
public static long copy(IOSupplier<InputStream> in, Path target, CopyOption... copyOptions) throws IOException Copies the InputStream from the supplier withFiles.copy(InputStream, Path, CopyOption...)
.- Parameters:
in
- Supplies the InputStream.target
- SeeFiles.copy(InputStream, Path, CopyOption...)
.copyOptions
- SeeFiles.copy(InputStream, Path, CopyOption...)
.- Returns:
- See
Files.copy(InputStream, Path, CopyOption...)
- Throws:
IOException
- SeeFiles.copy(InputStream, Path, CopyOption...)
- Since:
- 2.12.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
Counts aspects of a directory including subdirectories.- 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.
-
countDirectoryAsBigInteger
Counts aspects of a directory including subdirectories.- Parameters:
directory
- directory to count.- Returns:
- The visitor used to count the given directory.
- Throws:
IOException
- if an I/O error occurs.- Since:
- 2.12.0
-
createParentDirectories
Creates the parent directories for the givenpath
.If the parent directory already exists, then return it.
- Parameters:
path
- The path to a file (or directory).attrs
- An optional list of file attributes to set atomically when creating the directories.- Returns:
- The Path for the
path
's parent directory or null if the given path has no parent. - Throws:
IOException
- if an I/O error occurs.- Since:
- 2.9.0
-
createParentDirectories
public static Path createParentDirectories(Path path, LinkOption linkOption, FileAttribute<?>... attrs) throws IOException Creates the parent directories for the givenpath
.If the parent directory already exists, then return it.
- Parameters:
path
- The path to a file (or directory).linkOption
- ALinkOption
or null.attrs
- An optional list of file attributes to set atomically when creating the directories.- Returns:
- The Path for the
path
's parent directory or null if the given path has no parent. - Throws:
IOException
- if an I/O error occurs.- Since:
- 2.12.0
-
current
Gets the current directory.- Returns:
- the current directory.
- Since:
- 2.9.0
-
delete
Deletes a file or directory. If the path is a directory, delete it and all subdirectories.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:
NullPointerException
- if the directory isnull
IOException
- if an I/O error is thrown by a visitor method or if an I/O error occurs.
-
delete
public static Counters.PathCounters delete(Path path, DeleteOption... deleteOptions) throws IOException Deletes a file or directory. If the path is a directory, delete it and all subdirectories.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
deleteOptions
- How to handle deletion.- Returns:
- The visitor used to delete the given directory.
- Throws:
NullPointerException
- if the directory isnull
IOException
- if an I/O error is thrown by a visitor method or if an I/O error occurs.- Since:
- 2.8.0
-
delete
public static Counters.PathCounters delete(Path path, LinkOption[] linkOptions, DeleteOption... deleteOptions) throws IOException Deletes a file or directory. If the path is a directory, delete it and all subdirectories.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
linkOptions
- How to handle symbolic links.deleteOptions
- How to handle deletion.- Returns:
- The visitor used to delete the given directory.
- Throws:
NullPointerException
- if the directory isnull
IOException
- if an I/O error is thrown by a visitor method or if an I/O error occurs.- Since:
- 2.9.0
-
deleteDirectory
Deletes a directory including subdirectories.- Parameters:
directory
- directory to delete.- Returns:
- The visitor used to delete the given directory.
- Throws:
IOException
- if an I/O error is thrown by a visitor method.
-
deleteDirectory
public static Counters.PathCounters deleteDirectory(Path directory, DeleteOption... deleteOptions) throws IOException Deletes a directory including subdirectories.- Parameters:
directory
- directory to delete.deleteOptions
- How to handle deletion.- Returns:
- The visitor used to delete the given directory.
- Throws:
IOException
- if an I/O error is thrown by a visitor method.- Since:
- 2.8.0
-
deleteDirectory
public static Counters.PathCounters deleteDirectory(Path directory, LinkOption[] linkOptions, DeleteOption... deleteOptions) throws IOException Deletes a directory including subdirectories.- Parameters:
directory
- directory to delete.linkOptions
- How to handle symbolic links.deleteOptions
- How to handle deletion.- Returns:
- The visitor used to delete the given directory.
- Throws:
IOException
- if an I/O error is thrown by a visitor method.- Since:
- 2.9.0
-
deleteFile
Deletes the given file.- Parameters:
file
- The file to delete.- Returns:
- A visitor with path counts set to 1 file, 0 directories, and the size of the deleted file.
- Throws:
IOException
- if an I/O error occurs.NoSuchFileException
- if the file is a directory
-
deleteFile
public static Counters.PathCounters deleteFile(Path file, DeleteOption... deleteOptions) throws IOException Deletes the given file.- Parameters:
file
- The file to delete.deleteOptions
- How to handle deletion.- Returns:
- A visitor with path counts set to 1 file, 0 directories, and the size of the deleted file.
- Throws:
IOException
- if an I/O error occurs.NoSuchFileException
- if the file is a directory.- Since:
- 2.8.0
-
deleteFile
public static Counters.PathCounters deleteFile(Path file, LinkOption[] linkOptions, DeleteOption... deleteOptions) throws NoSuchFileException, IOException Deletes the given file.- Parameters:
file
- The file to delete.linkOptions
- How to handle symbolic links.deleteOptions
- How to handle deletion.- Returns:
- A visitor with path counts set to 1 file, 0 directories, and the size of the deleted file.
- Throws:
IOException
- if an I/O error occurs.NoSuchFileException
- if the file is a directory.- Since:
- 2.9.0
-
deleteOnExit
Delegates toFile.deleteOnExit()
.- Parameters:
path
- the path to delete.- Since:
- 3.13.0
-
directoryAndFileContentEquals
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 subdirectories.- 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 subdirectories.- 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
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 subdirectories.- 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 subdirectories.- 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:
IOException
- if an I/O error is thrown by a visitor method.
-
fileContentEquals
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
RandomAccessFileMode.create(Path)
.- Parameters:
path1
- the first stream.path2
- the second stream.linkOptions
- options specifying how files are followed.openOptions
- ignored.- Returns:
- true if the content of the streams are equal or they both don't exist, false otherwise.
- Throws:
NullPointerException
- if openOptions is null.IOException
- if an I/O error occurs.- See Also:
-
filter
Applies an
IOFileFilter
to the providedFile
objects. The resulting array is a subset of the original file list that matches the provided filter.The
Set
returned by this method is not guaranteed to be thread safe.Set<File> allFiles = ... Set<File> javaFiles = FileFilterUtils.filterSet(allFiles, FileFilterUtils.suffixFileFilter(".java"));
- Parameters:
filter
- the filter to apply to the set of files.paths
- the array of files to apply the filter to.- Returns:
- a subset of
files
that is accepted by the file filter. - Throws:
NullPointerException
- if the filter isnull
IllegalArgumentException
- iffiles
contains anull
value.- Since:
- 2.9.0
-
getAclEntryList
Reads the access control list from a file attribute view.- Parameters:
sourcePath
- the path to the file.- Returns:
- a file attribute view of the given type, or null if the attribute view type is not available.
- Throws:
IOException
- if an I/O error occurs.- Since:
- 2.8.0
-
getAclFileAttributeView
Shorthand forFiles.getFileAttributeView(path, AclFileAttributeView.class)
.- Parameters:
path
- the path to the file.options
- how to handle symbolic links.- Returns:
- a AclFileAttributeView, or
null
if the attribute view type is not available. - Since:
- 2.12.0
-
getBaseName
Gets the base name (the part up to and not including the last ".") of the last path segment of a file name.Will return the file name itself if it doesn't contain any dots. All leading directories of the
file name
parameter are skipped.- Parameters:
path
- the path of the file to obtain the base name of.- Returns:
- the base name of file name
- Since:
- 2.16.0
-
getDosFileAttributeView
Shorthand forFiles.getFileAttributeView(path, DosFileAttributeView.class, options)
.- Parameters:
path
- the path to the file.options
- how to handle symbolic links.- Returns:
- a DosFileAttributeView, or
null
if the attribute view type is not available. - Since:
- 2.12.0
-
getExtension
Gets the extension of a Path.This method returns the textual part of the Path after the last dot.
foo.txt --> "txt" a/b/c.jpg --> "jpg" a/b.txt/c --> "" a/b/c --> ""
The output will be the same irrespective of the machine that the code is running on.
- Parameters:
path
- the path to query.- Returns:
- the extension of the file or an empty string if none exists or
null
if the fileName isnull
. - Since:
- 2.16.0
-
getFileName
Gets the Path's file name and apply the given function if the file name is non-null.- Type Parameters:
R
- The function's result type.- Parameters:
path
- the path to query.function
- function to apply to the file name.- Returns:
- the Path's file name as a string or null.
- Since:
- 2.16.0
- See Also:
-
getFileNameString
Gets the Path's file name as a string.- Parameters:
path
- the path to query.- Returns:
- the Path's file name as a string or null.
- Since:
- 2.16.0
- See Also:
-
getLastModifiedFileTime
Gets the file's last modified time or null if the file does not exist.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.- Parameters:
file
- the file to query.- Returns:
- the file's last modified time.
- Throws:
IOException
- Thrown if an I/O error occurs.- Since:
- 2.12.0
-
getLastModifiedFileTime
public static FileTime getLastModifiedFileTime(Path path, FileTime defaultIfAbsent, LinkOption... options) throws IOException Gets the file's last modified time or null if the file does not exist.- Parameters:
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.- Returns:
- the file's last modified time.
- Throws:
IOException
- Thrown if an I/O error occurs.- Since:
- 2.12.0
-
getLastModifiedFileTime
Gets the file's last modified time or null if the file does not exist.- Parameters:
path
- the file to query.options
- options indicating how symbolic links are handled.- Returns:
- the file's last modified time.
- Throws:
IOException
- Thrown if an I/O error occurs.- Since:
- 2.12.0
-
getLastModifiedFileTime
Gets the file's last modified time or null if the file does not exist.- Parameters:
uri
- the file to query.- Returns:
- the file's last modified time.
- Throws:
IOException
- Thrown if an I/O error occurs.- Since:
- 2.12.0
-
getLastModifiedFileTime
Gets the file's last modified time or null if the file does not exist.- Parameters:
url
- the file to query.- Returns:
- the file's last modified time.
- Throws:
IOException
- Thrown if an I/O error occurs.URISyntaxException
- if the URL is not formatted strictly according to RFC2396 and cannot be converted to a URI.- Since:
- 2.12.0
-
getPosixFileAttributeView
Shorthand forFiles.getFileAttributeView(path, PosixFileAttributeView.class)
.- Parameters:
path
- the path to the file.options
- how to handle symbolic links.- Returns:
- a PosixFileAttributeView, or
null
if the attribute view type is not available. - Since:
- 2.12.0
-
getTempDirectory
Gets aPath
representing the system temporary directory.- Returns:
- the system temporary directory.
- Since:
- 2.12.0
-
isDirectory
Tests whether the givenPath
is a directory or not. Implemented as a null-safe delegate toFiles.isDirectory(Path path, LinkOption... options)
.- Parameters:
path
- the path to the file.options
- options indicating how to handle symbolic links- Returns:
true
if the file is a directory;false
if the path is null, the file does not exist, is not a directory, or it cannot be determined if the file is a directory or not.- Throws:
SecurityException
- In the case of the default provider, and a security manager is installed, thecheckRead
method is invoked to check read access to the directory.- Since:
- 2.9.0
-
isEmpty
Tests whether the given file or directory is empty.- Parameters:
path
- the file or directory to query.- Returns:
- whether the file or directory is empty.
- Throws:
IOException
- if an I/O error occurs.
-
isEmptyDirectory
Tests whether the directory is empty.- Parameters:
directory
- the directory to query.- Returns:
- whether the directory is empty.
- Throws:
NotDirectoryException
- if the file could not otherwise be opened because it is not a directory (optional specific exception).IOException
- if an I/O error occurs.SecurityException
- In the case of the default provider, and a security manager is installed, thecheckRead
method is invoked to check read access to the directory.
-
isEmptyFile
Tests whether the given file is empty.- Parameters:
file
- the file to query.- Returns:
- whether the file is empty.
- Throws:
IOException
- if an I/O error occurs.SecurityException
- In the case of the default provider, and a security manager is installed, itscheckRead
method denies read access to the file.
-
isNewer
public static boolean isNewer(Path file, ChronoZonedDateTime<?> czdt, LinkOption... options) throws IOException Tests if the givenPath
is newer than the given time reference.- Parameters:
file
- thePath
to test.czdt
- the time reference.options
- options indicating how to handle symbolic links.- Returns:
- true if the
Path
exists and has been modified after the given time reference. - Throws:
IOException
- if an I/O error occurs.NullPointerException
- if the file isnull
.- Since:
- 2.12.0
-
isNewer
public static boolean isNewer(Path file, FileTime fileTime, LinkOption... options) throws IOException Tests if the givenPath
is newer than the given time reference.- Parameters:
file
- thePath
to test.fileTime
- the time reference.options
- options indicating how to handle symbolic links.- Returns:
- true if the
Path
exists and has been modified after the given time reference. - Throws:
IOException
- if an I/O error occurs.NullPointerException
- if the file isnull
.- Since:
- 2.12.0
-
isNewer
Tests if the givenPath
is newer than the given time reference.- Parameters:
file
- thePath
to test.instant
- the time reference.options
- options indicating how to handle symbolic links.- Returns:
- true if the
Path
exists and has been modified after the given time reference. - Throws:
IOException
- if an I/O error occurs.NullPointerException
- if the file isnull
.- Since:
- 2.12.0
-
isNewer
Tests if the givenPath
is newer than the given time reference.- Parameters:
file
- thePath
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.- Returns:
- true if the
Path
exists and has been modified after the given time reference. - Throws:
IOException
- if an I/O error occurs.NullPointerException
- if the file isnull
.- Since:
- 2.9.0
-
isNewer
- Parameters:
file
- theFile
to test.reference
- theFile
of which the modification date is used.- Returns:
- true if the
File
exists and has been modified more recently than the referenceFile
. - Throws:
IOException
- if an I/O error occurs.- Since:
- 2.12.0
-
isOlder
public static boolean isOlder(Path file, FileTime fileTime, LinkOption... options) throws IOException Tests if the givenPath
is older than the given time reference.- Parameters:
file
- thePath
to test.fileTime
- the time reference.options
- options indicating how to handle symbolic links.- Returns:
- true if the
Path
exists and has been modified before the given time reference. - Throws:
IOException
- if an I/O error occurs.NullPointerException
- if the file isnull
.- Since:
- 2.12.0
-
isOlder
Tests if the givenPath
is older than the given time reference.- Parameters:
file
- thePath
to test.instant
- the time reference.options
- options indicating how to handle symbolic links.- Returns:
- true if the
Path
exists and has been modified before the given time reference. - Throws:
IOException
- if an I/O error occurs.NullPointerException
- if the file isnull
.- Since:
- 2.12.0
-
isOlder
Tests if the givenPath
is older than the given time reference.- Parameters:
file
- thePath
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.- Returns:
- true if the
Path
exists and has been modified before the given time reference. - Throws:
IOException
- if an I/O error occurs.NullPointerException
- if the file isnull
.- Since:
- 2.12.0
-
isOlder
- Parameters:
file
- theFile
to test.reference
- theFile
of which the modification date is used.- Returns:
- true if the
File
exists and has been modified before than the referenceFile
. - Throws:
IOException
- if an I/O error occurs.- Since:
- 2.12.0
-
isPosix
Tests whether the given path is on a POSIX file system.- Parameters:
test
- The Path to test.options
- options indicating how to handle symbolic links.- Returns:
- true if test is on a POSIX file system.
- Since:
- 2.12.0
-
isRegularFile
Tests whether the givenPath
is a regular file or not. Implemented as a null-safe delegate toFiles.isRegularFile(Path path, LinkOption... options)
.- Parameters:
path
- the path to the file.options
- options indicating how to handle symbolic links.- Returns:
true
if the file is a regular file;false
if the path is null, the file does not exist, is not a directory, or it cannot be determined if the file is a regular file or not.- Throws:
SecurityException
- In the case of the default provider, and a security manager is installed, thecheckRead
method is invoked to check read access to the directory.- Since:
- 2.9.0
-
newDirectoryStream
public static DirectoryStream<Path> newDirectoryStream(Path dir, PathFilter pathFilter) throws IOException Creates a new DirectoryStream for Paths rooted at the given directory.If you don't use the try-with-resources construct, then you must call the stream's
BaseStream.close()
method after iteration is complete to free any resources held for the open directory.- Parameters:
dir
- the path to the directory to stream.pathFilter
- the directory stream filter.- Returns:
- a new instance.
- Throws:
IOException
- if an I/O error occurs.
-
newOutputStream
Creates a new OutputStream by opening or creating a file, returning an output stream that may be used to write bytes to the file.- Parameters:
path
- the Path.append
- Whether or not to append.- Returns:
- a new OutputStream.
- Throws:
IOException
- if an I/O error occurs.- Since:
- 2.12.0
- See Also:
-
noFollowLinkOptionArray
Copy of theLinkOption
array forLinkOption.NOFOLLOW_LINKS
.- Returns:
- Copy of the
LinkOption
array forLinkOption.NOFOLLOW_LINKS
.
-
readAttributes
public static <A extends BasicFileAttributes> A readAttributes(Path path, Class<A> type, LinkOption... options) Reads the BasicFileAttributes from the given path. Returns null if the attributes can't be read.- Type Parameters:
A
- TheBasicFileAttributes
type- Parameters:
path
- The Path to test.type
- theClass
of the file attributes required to read.options
- options indicating how to handle symbolic links.- Returns:
- the file attributes or null if the attributes can't be read.
- Since:
- 2.12.0
- See Also:
-
readBasicFileAttributes
Reads the BasicFileAttributes from the given path.- Parameters:
path
- the path to read.- Returns:
- the path attributes.
- Throws:
IOException
- if an I/O error occurs.- Since:
- 2.9.0
-
readBasicFileAttributes
Reads the BasicFileAttributes from the given path. Returns null if the attributes can't be read.- Parameters:
path
- the path to read.options
- options indicating how to handle symbolic links.- Returns:
- the path attributes.
- Since:
- 2.12.0
-
readBasicFileAttributesUnchecked
Deprecated.Reads the BasicFileAttributes from the given path. Returns null if the attributes can't be read.- Parameters:
path
- the path to read.- Returns:
- the path attributes.
- Since:
- 2.9.0
-
readDosFileAttributes
Reads the DosFileAttributes from the given path. Returns null if the attributes can't be read.- Parameters:
path
- the path to read.options
- options indicating how to handle symbolic links.- Returns:
- the path attributes.
- Since:
- 2.12.0
-
readOsFileAttributes
Reads the PosixFileAttributes or DosFileAttributes from the given path. Returns null if the attributes can't be read.- Parameters:
path
- The Path to read.options
- options indicating how to handle symbolic links.- Returns:
- the file attributes.
- Since:
- 2.12.0
-
readPosixFileAttributes
Reads the PosixFileAttributes from the given path. Returns null instead of throwingUnsupportedOperationException
.- Parameters:
path
- The Path to read.options
- options indicating how to handle symbolic links.- Returns:
- the file attributes.
- Since:
- 2.12.0
-
readString
Reads the file contents at the given path as a String using the Charset.- Parameters:
path
- The source path.charset
- How to convert bytes to a String, null uses the default Charset.- Returns:
- the file contents as a new String.
- Throws:
IOException
- if an I/O error occurs reading from the stream.- Since:
- 2.12.0
- See Also:
-
setLastModifiedTime
Sets the giventargetFile
's last modified time to the value fromsourceFile
.- Parameters:
sourceFile
- The source path to query.targetFile
- The target path to set.- Throws:
NullPointerException
- if sourceFile isnull
.NullPointerException
- if targetFile isnull
.IOException
- if setting the last-modified time failed.- Since:
- 2.12.0
-
setReadOnly
public static Path setReadOnly(Path path, boolean readOnly, LinkOption... linkOptions) throws IOException Sets the given Path to thereadOnly
value.This behavior is OS dependent.
- Parameters:
path
- The path to set.readOnly
- true for read-only, false for not read-only.linkOptions
- options indicating how to handle symbolic links.- Returns:
- The given path.
- Throws:
IOException
- if an I/O error occurs.- Since:
- 2.8.0
-
sizeOf
Returns the size of the given file or directory. If the providedPath
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.- Parameters:
path
- the regular file or directory to return the size of, must not benull
.- Returns:
- the length of the file, or recursive size of the directory, in bytes.
- Throws:
NullPointerException
- if the file isnull
.IllegalArgumentException
- if the file does not exist.IOException
- if an I/O error occurs.- Since:
- 2.12.0
-
sizeOfAsBigInteger
Returns the size of the given file or directory. If the providedPath
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.- Parameters:
path
- the regular file or directory to return the size of (must not benull
).- Returns:
- the length of the file, or recursive size of the directory, provided (in bytes).
- Throws:
NullPointerException
- if the file isnull
.IllegalArgumentException
- if the file does not exist.IOException
- if an I/O error occurs.- Since:
- 2.12.0
-
sizeOfDirectory
Counts the size of a directory recursively (sum of the size of all files).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.- Parameters:
directory
- directory to inspect, must not benull
.- Returns:
- size of directory in bytes, 0 if directory is security restricted, a negative number when the real total is greater than
Long.MAX_VALUE
. - Throws:
NullPointerException
- if the directory isnull
.IOException
- if an I/O error occurs.- Since:
- 2.12.0
-
sizeOfDirectoryAsBigInteger
Counts the size of a directory recursively (sum of the size of all files).- Parameters:
directory
- directory to inspect, must not benull
.- Returns:
- size of directory in bytes, 0 if directory is security restricted.
- Throws:
NullPointerException
- if the directory isnull
.IOException
- if an I/O error occurs.- Since:
- 2.12.0
-
touch
Implements behavior similar to the UNIX "touch" utility. Creates a new file with size 0, or, if the file exists, just updates the file's modified time. this method creates parent directories if they do not exist.- Parameters:
file
- the file to touch.- Returns:
- The given file.
- Throws:
NullPointerException
- if the parameter isnull
.IOException
- if setting the last-modified time failed or an I/O problem occurs.\- Since:
- 2.12.0
-
visitFileTree
public static <T extends FileVisitor<? super Path>> T visitFileTree(T visitor, Path directory) throws IOException PerformsFiles.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:
NoSuchFileException
- if the directory does not exist.IOException
- if an I/O error is thrown by a visitor method.NullPointerException
- if the directory isnull
.
-
visitFileTree
public static <T extends FileVisitor<? super Path>> T visitFileTree(T visitor, Path start, Set<FileVisitOption> options, int maxDepth) throws IOException PerformsFiles.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:
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 PerformsFiles.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:
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 PerformsFiles.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:
IOException
- if an I/O error is thrown by a visitor method.
-
waitFor
Waits for the file system to detect a file's presence, with a timeout.This method repeatedly tests
Files.exists(Path,LinkOption...)
until it returns true up to the maximum time given.- Parameters:
file
- the file to check, must not benull
.timeout
- the maximum time to wait.options
- options indicating how to handle symbolic links.- Returns:
- true if file exists.
- Throws:
NullPointerException
- if the file isnull
.- Since:
- 2.12.0
-
walk
public static Stream<Path> walk(Path start, PathFilter pathFilter, int maxDepth, boolean readAttributes, FileVisitOption... options) throws IOException Returns a stream of filtered paths.The returned
Stream
may wrap one or moreDirectoryStream
s. When you require timely disposal of file system resources, use atry
-with-resources block to ensure invocation of the stream'sBaseStream.close()
method after the stream operations are completed. Calling a closed stream causes aIllegalStateException
.- Parameters:
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.- Returns:
- a filtered stream of paths.
- Throws:
IOException
- if an I/O error is thrown when accessing the starting file.- Since:
- 2.9.0
-
writeString
public static Path writeString(Path path, CharSequence charSequence, Charset charset, OpenOption... openOptions) throws IOException Writes the given character sequence to a file at the given path.- Parameters:
path
- The target file.charSequence
- The character sequence text.charset
- The Charset to encode the text.openOptions
- options How to open the file.- Returns:
- The given path.
- Throws:
IOException
- if an I/O error occurs writing to or creating the file.NullPointerException
- if eitherpath
orcharSequence
isnull
.- Since:
- 2.12.0
-
noFollowLinkOptionArray()
.