Class PathHelper
- Author:
- Philip Helger
-
Method Summary
Modifier and TypeMethodDescriptionstatic booleancanReadAndWriteFile(@Nullable Path aFile) Check if the passed file can read and write.static @NonNull com.helger.base.state.EChangeensureParentDirectoryIsPresent(@NonNull Path aFile) Ensure that the parent directory of the passed path is present.static booleanequalPaths(@NonNull Path aObj1, @NonNull Path aObj2) Compare two paths for equality by resolving their real paths.static @Nullable URLConvert the passed path to aURL.static @Nullable com.helger.base.io.nonblocking.NonBlockingBufferedInputStreamgetBufferedInputStream(@NonNull Path aFile) Get a buffered input stream for the passed path.static @Nullable com.helger.base.io.nonblocking.NonBlockingBufferedOutputStreamgetBufferedOutputStream(@NonNull Path aFile) Get a buffered output stream for the passed path using the default append mode.static @Nullable com.helger.base.io.nonblocking.NonBlockingBufferedOutputStreamgetBufferedOutputStream(@NonNull Path aFile, @NonNull com.helger.base.io.EAppend eAppend) Get a buffered output stream for the passed path.static @Nullable com.helger.base.io.nonblocking.NonBlockingBufferedReadergetBufferedReader(@NonNull Path aFile, @NonNull Charset aCharset) Get a bufferedReaderfor the passed path using the specified charset.static @Nullable com.helger.base.io.nonblocking.NonBlockingBufferedWritergetBufferedWriter(@NonNull Path aFile, @NonNull com.helger.base.io.EAppend eAppend, @NonNull Charset aCharset) Get a bufferedWriterfor the passed path using the specified append mode and charset.static @Nullable PathgetCanonicalFile(@Nullable Path aFile) Get the canonical file of the passed file, if the file is notnull.static @Nullable PathgetCanonicalFileOrNull(@Nullable Path aFile) Get the canonical file of the passed file, if the file is notnull.static @Nullable StringgetCanonicalPath(@Nullable Path aFile) Get the canonical path of the passed file, if the file is notnull.static @Nullable StringgetCanonicalPathOrNull(@Nullable Path aFile) Get the canonical path of the passed file, if the file is notnull.static @NonNull com.helger.collection.commons.ICommonsList<Path> getDirectoryContent(@NonNull Path aDirectory) This is a replacement forPath.listFiles()doing some additional checks on permissions.static @NonNull com.helger.collection.commons.ICommonsList<Path> getDirectoryContent(@NonNull Path aDirectory, @Nullable Predicate<? super Path> aPathFilter) This is a replacement forPath.listFiles(FilenameFilter)doing some additional checks on permissions.static intgetDirectoryObjectCount(@NonNull Path aDirectory) Returns the number of files and directories contained in the passed directory excluding the system internal directories.static @Nullable InputStreamgetInputStream(@NonNull Path aFile) Get anInputStreamfor the passed path.static @Nullable OutputStreamgetOutputStream(@NonNull Path aFile) Get an output stream for writing to a file.static @Nullable OutputStreamgetOutputStream(@NonNull Path aFile, @NonNull com.helger.base.io.EAppend eAppend) Get an output stream for writing to a file.static @Nullable ReaderGet aReaderfor the passed path using the specified charset.static @Nullable WritergetWriter(@NonNull Path aFile, @NonNull com.helger.base.io.EAppend eAppend, @NonNull Charset aCharset) Get aWriterfor the passed path using the specified append mode and charset.static booleanisFileNewer(@NonNull Path aFile1, @NonNull Path aFile2) Returnstrueif the first file is newer than the second file.static booleanisParentDirectory(@NonNull Path aSearchDirectory, @NonNull Path aStartDirectory) Check if the searched directory is a parent object of the start directorystatic @NonNull PathwalkFileTree(@NonNull Path aStart, int nMaxDepth, @NonNull FileVisitor<? super Path> aVisitor) Walk a file tree with noFileVisitOptions.static @NonNull PathwalkFileTree(@NonNull Path aStart, @NonNull Set<FileVisitOption> aOptions, int nMaxDepth, @NonNull FileVisitor<? super Path> aVisitor) Walks a file tree.
-
Method Details
-
equalPaths
Compare two paths for equality by resolving their real paths.- Parameters:
aObj1- The first path. May not benull.aObj2- The second path. May not benull.- Returns:
trueif the two paths point to the same location,falseotherwise.
-
canReadAndWriteFile
Check if the passed file can read and write. If the file already exists, the file itself is checked. If the file does not exist, the parent directory- Parameters:
aFile- The file to be checked. May benull.- Returns:
trueif the file can be read and written
-
ensureParentDirectoryIsPresent
public static @NonNull com.helger.base.state.EChange ensureParentDirectoryIsPresent(@NonNull Path aFile) Ensure that the parent directory of the passed path is present. If it is not present, it will be created.- Parameters:
aFile- The path whose parent directory should be present. May not benull.- Returns:
EChange.CHANGEDif the parent directory was created,EChange.UNCHANGEDif it already existed.- Throws:
IllegalStateException- If the parent directory could not be created.
-
getCanonicalFile
Get the canonical file of the passed file, if the file is notnull.- Parameters:
aFile- The file to get the canonical path from. May benull.- Returns:
nullif the passed file isnull.- Throws:
IOException- If an I/O error occurs, which is possible because the construction of the canonical pathname may require filesystem queries
-
getCanonicalFileOrNull
Get the canonical file of the passed file, if the file is notnull. In case of anIOException,nullis returned.- Parameters:
aFile- The file to get the canonical path from. May benull.- Returns:
nullif the passed file isnullor an exception occurred.
-
getCanonicalPath
Get the canonical path of the passed file, if the file is notnull.- Parameters:
aFile- The file to get the canonical path from. May benull.- Returns:
nullif the passed file isnull.- Throws:
IOException- If an I/O error occurs, which is possible because the construction of the canonical pathname may require filesystem queries
-
getCanonicalPathOrNull
Get the canonical path of the passed file, if the file is notnull. In case of anIOException,nullis returned.- Parameters:
aFile- The file to get the canonical path from. May benull.- Returns:
nullif the passed file isnull.
-
isParentDirectory
public static boolean isParentDirectory(@NonNull Path aSearchDirectory, @NonNull Path aStartDirectory) Check if the searched directory is a parent object of the start directory- Parameters:
aSearchDirectory- The directory to be searched. May not benull.aStartDirectory- The directory where the search starts. May not benull.- Returns:
trueif the search directory is a parent of the start directory,falseotherwise.- See Also:
-
getInputStream
Get anInputStreamfor the passed path.- Parameters:
aFile- The path to open for reading. May not benull.- Returns:
nullif the path does not exist or cannot be opened.
-
getBufferedInputStream
public static @Nullable com.helger.base.io.nonblocking.NonBlockingBufferedInputStream getBufferedInputStream(@NonNull Path aFile) Get a buffered input stream for the passed path.- Parameters:
aFile- The path to open for reading. May not benull.- Returns:
nullif the path does not exist or cannot be opened.
-
getReader
Get aReaderfor the passed path using the specified charset.- Parameters:
aFile- The path to open for reading. May not benull.aCharset- The charset to use. May not benull.- Returns:
nullif the path does not exist or cannot be opened.
-
getBufferedReader
public static @Nullable com.helger.base.io.nonblocking.NonBlockingBufferedReader getBufferedReader(@NonNull Path aFile, @NonNull Charset aCharset) Get a bufferedReaderfor the passed path using the specified charset.- Parameters:
aFile- The path to open for reading. May not benull.aCharset- The charset to use. May not benull.- Returns:
nullif the path does not exist or cannot be opened.
-
getOutputStream
Get an output stream for writing to a file.- Parameters:
aFile- The file to write to. May not benull.- Returns:
nullif the file could not be opened
-
getOutputStream
public static @Nullable OutputStream getOutputStream(@NonNull Path aFile, @NonNull com.helger.base.io.EAppend eAppend) Get an output stream for writing to a file.- Parameters:
aFile- The file to write to. May not benull.eAppend- Appending mode. May not benull.- Returns:
nullif the file could not be opened
-
getBufferedOutputStream
public static @Nullable com.helger.base.io.nonblocking.NonBlockingBufferedOutputStream getBufferedOutputStream(@NonNull Path aFile) Get a buffered output stream for the passed path using the default append mode.- Parameters:
aFile- The path to write to. May not benull.- Returns:
nullif the path could not be opened.
-
getBufferedOutputStream
public static @Nullable com.helger.base.io.nonblocking.NonBlockingBufferedOutputStream getBufferedOutputStream(@NonNull Path aFile, @NonNull com.helger.base.io.EAppend eAppend) Get a buffered output stream for the passed path.- Parameters:
aFile- The path to write to. May not benull.eAppend- Appending mode. May not benull.- Returns:
nullif the path could not be opened.
-
getWriter
public static @Nullable Writer getWriter(@NonNull Path aFile, @NonNull com.helger.base.io.EAppend eAppend, @NonNull Charset aCharset) Get aWriterfor the passed path using the specified append mode and charset.- Parameters:
aFile- The path to write to. May not benull.eAppend- Appending mode. May not benull.aCharset- The charset to use. May not benull.- Returns:
nullif the path could not be opened.
-
getBufferedWriter
public static @Nullable com.helger.base.io.nonblocking.NonBlockingBufferedWriter getBufferedWriter(@NonNull Path aFile, @NonNull com.helger.base.io.EAppend eAppend, @NonNull Charset aCharset) Get a bufferedWriterfor the passed path using the specified append mode and charset.- Parameters:
aFile- The path to write to. May not benull.eAppend- Appending mode. May not benull.aCharset- The charset to use. May not benull.- Returns:
nullif the path could not be opened.
-
isFileNewer
Returnstrueif the first file is newer than the second file. Returnstrueif the first file exists and the second file does not exist. Returnsfalseif the first file is older than the second file. Returnsfalseif the first file does not exists but the second does. Returnsfalseif none of the files exist.- Parameters:
aFile1- First file. May not benull.aFile2- Second file. May not benull.- Returns:
trueif the first file is newer than the second file,falseotherwise.
-
getDirectoryObjectCount
Returns the number of files and directories contained in the passed directory excluding the system internal directories.- Parameters:
aDirectory- The directory to check. May not benulland must be a directory.- Returns:
- A non-negative number of objects in that directory.
- See Also:
-
walkFileTree
public static @NonNull Path walkFileTree(@NonNull Path aStart, @Nonnegative int nMaxDepth, @NonNull FileVisitor<? super Path> aVisitor) Walk a file tree with noFileVisitOptions.- Parameters:
aStart- The starting path. May not benull.nMaxDepth- The maximum number of directory levels to visit. Must be ≥ 0.aVisitor- The file visitor to invoke for each file. May not benull.- Returns:
- The starting path. Never
null. - Throws:
UncheckedIOException- if an I/O error is thrown by a visitor method
-
walkFileTree
public static @NonNull Path walkFileTree(@NonNull Path aStart, @NonNull Set<FileVisitOption> aOptions, @Nonnegative int nMaxDepth, @NonNull FileVisitor<? super Path> aVisitor) Walks a file tree.This method walks a file tree rooted at a given starting file. The file tree traversal is depth-first with the given
FileVisitorinvoked 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 ofTERMINATE. Where a visit method terminates due anIOException, 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 thevisitFilemethod is invoked with the file attributes. If the file attributes cannot be read, due to an I/O exception, then thevisitFileFailedmethod is invoked with the I/O exception.Where the file is a directory, and the directory could not be opened, then the
visitFileFailedmethod 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
postVisitDirectorymethod 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
optionsparameter contains theFOLLOW_LINKSoption then symbolic links are followed. When following links, and the attributes of the target cannot be read, then this method attempts to get theBasicFileAttributesof the link. If they can be read then thevisitFilemethod is invoked with the attributes of the link (otherwise thevisitFileFailedmethod is invoked as specified above).If the
optionsparameter contains theFOLLOW_LINKSoption 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 thefile-keyof directories, or if file keys are not available, by invoking theFiles.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 thevisitFileFailedmethod is invoked with an instance ofFileSystemLoopException.The
maxDepthparameter is the maximum number of levels of directories to visit. A value of0means that only the starting file is visited, unless denied by the security manager. A value ofMAX_VALUEmay be used to indicate that all levels should be visited. ThevisitFilemethod is invoked for all files, including directories, encountered atmaxDepth, unless the basic file attributes cannot be read, in which case thevisitFileFailedmethod is invoked.If a visitor returns a result of
nullthenNullPointerExceptionis 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).
- Parameters:
aStart- the starting fileaOptions- options to configure the traversalnMaxDepth- the maximum number of directory levels to visitaVisitor- the file visitor to invoke for each file- Returns:
- the starting file
- Throws:
UncheckedIOException- if an I/O error is thrown by a visitor method
-
getDirectoryContent
@ReturnsMutableCopy public static @NonNull com.helger.collection.commons.ICommonsList<Path> getDirectoryContent(@NonNull Path aDirectory) This is a replacement forPath.listFiles()doing some additional checks on permissions. The order of the returned files is undefined. "." and ".." are not contained.- Parameters:
aDirectory- The directory to be listed. May not benull.- Returns:
- Never
null.
-
getDirectoryContent
@ReturnsMutableCopy public static @NonNull com.helger.collection.commons.ICommonsList<Path> getDirectoryContent(@NonNull Path aDirectory, @Nullable Predicate<? super Path> aPathFilter) This is a replacement forPath.listFiles(FilenameFilter)doing some additional checks on permissions. The order of the returned files is undefined. "." and ".." are not contained.- Parameters:
aDirectory- The directory to be listed. May not benull.aPathFilter- The path filter to be used. May not benull.- Returns:
- Never
null.
-
getAsURL
Convert the passed path to aURL.- Parameters:
aPath- The path to be converted. May not benull.- Returns:
nullif the conversion to URL failed.
-