Class PathHelper
- java.lang.Object
-
- com.helger.commons.io.file.PathHelper
-
@Immutable public final class PathHelper extends Object
Miscellaneous file utility methods.- Author:
- Philip Helger
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static booleancanReadAndWriteFile(Path aFile)Check if the passed file can read and write.static EChangeensureParentDirectoryIsPresent(Path aFile)static URLgetAsURL(Path aPath)static NonBlockingBufferedInputStreamgetBufferedInputStream(Path aFile)static NonBlockingBufferedOutputStreamgetBufferedOutputStream(Path aFile)static NonBlockingBufferedOutputStreamgetBufferedOutputStream(Path aFile, EAppend eAppend)static NonBlockingBufferedReadergetBufferedReader(Path aFile, Charset aCharset)static NonBlockingBufferedWritergetBufferedWriter(Path aFile, EAppend eAppend, Charset aCharset)static PathgetCanonicalFile(Path aFile)Get the canonical file of the passed file, if the file is notnull.static PathgetCanonicalFileOrNull(Path aFile)Get the canonical file of the passed file, if the file is notnull.static StringgetCanonicalPath(Path aFile)Get the canonical path of the passed file, if the file is notnull.static StringgetCanonicalPathOrNull(Path aFile)Get the canonical path of the passed file, if the file is notnull.static ICommonsList<Path>getDirectoryContent(Path aDirectory)This is a replacement forPath.listFiles()doing some additional checks on permissions.static ICommonsList<Path>getDirectoryContent(Path aDirectory, Predicate<? super Path> aPathFilter)This is a replacement forPath.listFiles(FilenameFilter)doing some additional checks on permissions.static intgetDirectoryObjectCount(Path aDirectory)Returns the number of files and directories contained in the passed directory excluding the system internal directories.static InputStreamgetInputStream(Path aFile)static OutputStreamgetOutputStream(Path aFile)Get an output stream for writing to a file.static OutputStreamgetOutputStream(Path aFile, EAppend eAppend)Get an output stream for writing to a file.static ReadergetReader(Path aFile, Charset aCharset)static WritergetWriter(Path aFile, EAppend eAppend, Charset aCharset)static booleanisFileNewer(Path aFile1, Path aFile2)Returnstrueif the first file is newer than the second file.static booleanisParentDirectory(Path aSearchDirectory, Path aStartDirectory)Check if the searched directory is a parent object of the start directorystatic PathwalkFileTree(Path aStart, int nMaxDepth, FileVisitor<? super Path> aVisitor)static PathwalkFileTree(Path aStart, Set<FileVisitOption> aOptions, int nMaxDepth, FileVisitor<? super Path> aVisitor)Walks a file tree.
-
-
-
Method Detail
-
canReadAndWriteFile
public static boolean canReadAndWriteFile(@Nullable Path aFile)
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
@Nonnull public static EChange ensureParentDirectoryIsPresent(@Nonnull Path aFile)
-
getCanonicalFile
@Nullable public static Path getCanonicalFile(@Nullable Path aFile) throws IOException
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
@Nullable public static Path getCanonicalFileOrNull(@Nullable Path aFile)
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
@Nullable public static String getCanonicalPath(@Nullable Path aFile) throws IOException
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
@Nullable public static String getCanonicalPathOrNull(@Nullable Path aFile)
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:
getCanonicalFile(Path)
-
getInputStream
@Nullable public static InputStream getInputStream(@Nonnull Path aFile)
-
getBufferedInputStream
@Nullable public static NonBlockingBufferedInputStream getBufferedInputStream(@Nonnull Path aFile)
-
getBufferedReader
@Nullable public static NonBlockingBufferedReader getBufferedReader(@Nonnull Path aFile, @Nonnull Charset aCharset)
-
getOutputStream
@Nullable public static OutputStream getOutputStream(@Nonnull Path aFile)
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
@Nullable public static OutputStream getOutputStream(@Nonnull Path aFile, @Nonnull 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
@Nullable public static NonBlockingBufferedOutputStream getBufferedOutputStream(@Nonnull Path aFile)
-
getBufferedOutputStream
@Nullable public static NonBlockingBufferedOutputStream getBufferedOutputStream(@Nonnull Path aFile, @Nonnull EAppend eAppend)
-
getWriter
@Nullable public static Writer getWriter(@Nonnull Path aFile, @Nonnull EAppend eAppend, @Nonnull Charset aCharset)
-
getBufferedWriter
@Nullable public static NonBlockingBufferedWriter getBufferedWriter(@Nonnull Path aFile, @Nonnull EAppend eAppend, @Nonnull Charset aCharset)
-
isFileNewer
public static boolean isFileNewer(@Nonnull Path aFile1, @Nonnull Path aFile2)
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
@Nonnegative public static int getDirectoryObjectCount(@Nonnull Path aDirectory)
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:
FilenameHelper.isSystemInternalDirectory(CharSequence)
-
walkFileTree
@Nonnull public static Path walkFileTree(@Nonnull Path aStart, @Nonnegative int nMaxDepth, @Nonnull FileVisitor<? super Path> aVisitor)
-
walkFileTree
@Nonnull public static 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
@Nonnull @ReturnsMutableCopy public static 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
@Nonnull @ReturnsMutableCopy public static 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.
-
-