@Immutable public final class PathHelper extends Object
| Modifier and Type | Method and Description |
|---|---|
static boolean |
canReadAndWriteFile(Path aFile)
Check if the passed file can read and write.
|
static EChange |
ensureParentDirectoryIsPresent(Path aFile) |
static URL |
getAsURL(Path aPath) |
static NonBlockingBufferedInputStream |
getBufferedInputStream(Path aFile) |
static NonBlockingBufferedOutputStream |
getBufferedOutputStream(Path aFile) |
static NonBlockingBufferedOutputStream |
getBufferedOutputStream(Path aFile,
EAppend eAppend) |
static NonBlockingBufferedReader |
getBufferedReader(Path aFile,
Charset aCharset) |
static NonBlockingBufferedWriter |
getBufferedWriter(Path aFile,
EAppend eAppend,
Charset aCharset) |
static Path |
getCanonicalFile(Path aFile)
Get the canonical file of the passed file, if the file is not
null. |
static Path |
getCanonicalFileOrNull(Path aFile)
Get the canonical file of the passed file, if the file is not
null. |
static String |
getCanonicalPath(Path aFile)
Get the canonical path of the passed file, if the file is not
null. |
static String |
getCanonicalPathOrNull(Path aFile)
Get the canonical path of the passed file, if the file is not
null. |
static ICommonsList<Path> |
getDirectoryContent(Path aDirectory)
This is a replacement for
Path.listFiles() doing some
additional checks on permissions. |
static ICommonsList<Path> |
getDirectoryContent(Path aDirectory,
Predicate<? super Path> aPathFilter)
This is a replacement for
Path.listFiles(FilenameFilter) doing
some additional checks on permissions. |
static int |
getDirectoryObjectCount(Path aDirectory)
Returns the number of files and directories contained in the passed
directory excluding the system internal directories.
|
static InputStream |
getInputStream(Path aFile) |
static OutputStream |
getOutputStream(Path aFile)
Get an output stream for writing to a file.
|
static OutputStream |
getOutputStream(Path aFile,
EAppend eAppend)
Get an output stream for writing to a file.
|
static Reader |
getReader(Path aFile,
Charset aCharset) |
static Writer |
getWriter(Path aFile,
EAppend eAppend,
Charset aCharset) |
static boolean |
isFileNewer(Path aFile1,
Path aFile2)
Returns
true if the first file is newer than the second file. |
static boolean |
isParentDirectory(Path aSearchDirectory,
Path aStartDirectory)
Check if the searched directory is a parent object of the start directory
|
static Path |
walkFileTree(Path aStart,
int nMaxDepth,
FileVisitor<? super Path> aVisitor) |
static Path |
walkFileTree(Path aStart,
Set<FileVisitOption> aOptions,
int nMaxDepth,
FileVisitor<? super Path> aVisitor)
Walks a file tree.
|
public static boolean canReadAndWriteFile(@Nullable Path aFile)
aFile - The file to be checked. May be null.true if the file can be read and written@Nonnull public static EChange ensureParentDirectoryIsPresent(@Nonnull Path aFile)
@Nullable public static Path getCanonicalFile(@Nullable Path aFile) throws IOException
null.aFile - The file to get the canonical path from. May be null.null if the passed file is null.IOException - If an I/O error occurs, which is possible because the construction
of the canonical pathname may require filesystem queries@Nullable public static Path getCanonicalFileOrNull(@Nullable Path aFile)
null. In case of an IOException, null is
returned.aFile - The file to get the canonical path from. May be null.null if the passed file is null or an
exception occurred.@Nullable public static String getCanonicalPath(@Nullable Path aFile) throws IOException
null.aFile - The file to get the canonical path from. May be null.null if the passed file is null.IOException - If an I/O error occurs, which is possible because the construction
of the canonical pathname may require filesystem queries@Nullable public static String getCanonicalPathOrNull(@Nullable Path aFile)
null. In case of an IOException, null is
returned.aFile - The file to get the canonical path from. May be null.null if the passed file is null.public static boolean isParentDirectory(@Nonnull Path aSearchDirectory, @Nonnull Path aStartDirectory)
aSearchDirectory - The directory to be searched. May not be null.aStartDirectory - The directory where the search starts. May not be null.true if the search directory is a parent of the start
directory, false otherwise.getCanonicalFile(Path)@Nullable public static InputStream getInputStream(@Nonnull Path aFile)
@Nullable public static NonBlockingBufferedInputStream getBufferedInputStream(@Nonnull Path aFile)
@Nullable public static NonBlockingBufferedReader getBufferedReader(@Nonnull Path aFile, @Nonnull Charset aCharset)
@Nullable public static OutputStream getOutputStream(@Nonnull Path aFile)
aFile - The file to write to. May not be null.null if the file could not be opened@Nullable public static OutputStream getOutputStream(@Nonnull Path aFile, @Nonnull EAppend eAppend)
aFile - The file to write to. May not be null.eAppend - Appending mode. May not be null.null if the file could not be opened@Nullable public static NonBlockingBufferedOutputStream getBufferedOutputStream(@Nonnull Path aFile)
@Nullable public static NonBlockingBufferedOutputStream getBufferedOutputStream(@Nonnull Path aFile, @Nonnull EAppend eAppend)
@Nullable public static Writer getWriter(@Nonnull Path aFile, @Nonnull EAppend eAppend, @Nonnull Charset aCharset)
@Nullable public static NonBlockingBufferedWriter getBufferedWriter(@Nonnull Path aFile, @Nonnull EAppend eAppend, @Nonnull Charset aCharset)
public static boolean isFileNewer(@Nonnull Path aFile1, @Nonnull Path aFile2)
true if the first file is newer than the second file.
Returns true if the first file exists and the second file does
not exist. Returns false if the first file is older than the
second file. Returns false if the first file does not exists
but the second does. Returns false if none of the files exist.aFile1 - First file. May not be null.aFile2 - Second file. May not be null.true if the first file is newer than the second file,
false otherwise.@Nonnegative public static int getDirectoryObjectCount(@Nonnull Path aDirectory)
aDirectory - The directory to check. May not be null and must be a
directory.FilenameHelper.isSystemInternalDirectory(CharSequence)@Nonnull public static Path walkFileTree(@Nonnull Path aStart, @Nonnegative int nMaxDepth, @Nonnull FileVisitor<? super Path> aVisitor)
@Nonnull public static Path walkFileTree(@Nonnull Path aStart, @Nonnull Set<FileVisitOption> aOptions, @Nonnegative int nMaxDepth, @Nonnull FileVisitor<? super Path> aVisitor)
This method walks a file tree rooted at a given starting file. The file
tree traversal is depth-first with the given FileVisitor
invoked for each file encountered. File tree traversal completes when all
accessible files in the tree have been visited, or a visit method returns a
result of TERMINATE. Where a visit method
terminates due an IOException, an uncaught error, or runtime
exception, then the traversal is terminated and the error or exception is
propagated to the caller of this method.
For each file encountered this method attempts to read its
BasicFileAttributes. If the file is not a
directory then the visitFile method is
invoked with the file attributes. If the file attributes cannot be read,
due to an I/O exception, then the visitFileFailed method is invoked with the I/O exception.
Where the file is a directory, and the directory could not be opened, then
the visitFileFailed method is invoked with the I/O exception, after
which, the file tree walk continues, by default, at the next
sibling of the directory.
Where the directory is opened successfully, then the entries in the
directory, and their descendants are visited. When all entries
have been visited, or an I/O error occurs during iteration of the
directory, then the directory is closed and the visitor's
postVisitDirectory method is
invoked. The file tree walk then continues, by default, at the next
sibling of the directory.
By default, symbolic links are not automatically followed by this method.
If the options parameter contains the
FOLLOW_LINKS option then symbolic
links are followed. When following links, and the attributes of the target
cannot be read, then this method attempts to get the
BasicFileAttributes of the link. If they can be read then the
visitFile method is invoked with the attributes of the link
(otherwise the visitFileFailed method is invoked as specified
above).
If the options parameter contains the
FOLLOW_LINKS option then this method
keeps track of directories visited so that cycles can be detected. A cycle
arises when there is an entry in a directory that is an ancestor of the
directory. Cycle detection is done by recording the
file-key of
directories, or if file keys are not available, by invoking the
Files.isSameFile(java.nio.file.Path, java.nio.file.Path) method to test if a directory is the same file as
an ancestor. When a cycle is detected it is treated as an I/O error, and
the visitFileFailed method is invoked
with an instance of FileSystemLoopException.
The maxDepth parameter is the maximum number of levels of
directories to visit. A value of 0 means that only the starting
file is visited, unless denied by the security manager. A value of
MAX_VALUE may be used to indicate that all levels
should be visited. The visitFile method is invoked for all files,
including directories, encountered at maxDepth, unless the basic
file attributes cannot be read, in which case the visitFileFailed method is invoked.
If a visitor returns a result of null then NullPointerException is thrown.
When a security manager is installed and it denies access to a file (or directory), then it is ignored and the visitor is not invoked for that file (or directory).
aStart - the starting fileaOptions - options to configure the traversalnMaxDepth - the maximum number of directory levels to visitaVisitor - the file visitor to invoke for each fileUncheckedIOException - if an I/O error is thrown by a visitor method@Nonnull @ReturnsMutableCopy public static ICommonsList<Path> getDirectoryContent(@Nonnull Path aDirectory)
Path.listFiles() doing some
additional checks on permissions. The order of the returned files is
undefined. "." and ".." are not contained.aDirectory - The directory to be listed. May not be null.null.@Nonnull @ReturnsMutableCopy public static ICommonsList<Path> getDirectoryContent(@Nonnull Path aDirectory, @Nullable Predicate<? super Path> aPathFilter)
Path.listFiles(FilenameFilter) doing
some additional checks on permissions. The order of the returned files is
undefined. "." and ".." are not contained.aDirectory - The directory to be listed. May not be null.aPathFilter - The path filter to be used. May not be null.null.Copyright © 2014–2022 Philip Helger. All rights reserved.