Class PathHelper


  • @Immutable
    public final class PathHelper
    extends Object
    Miscellaneous file utility methods.
    Author:
    Philip Helger
    • 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 be null.
        Returns:
        true if the file can be read and written
      • getCanonicalFile

        @Nullable
        public static Path getCanonicalFile​(@Nullable
                                            Path aFile)
                                     throws IOException
        Get the canonical file of the passed file, if the file is not null.
        Parameters:
        aFile - The file to get the canonical path from. May be null.
        Returns:
        null if the passed file is null.
        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 not null. In case of an IOException, null is returned.
        Parameters:
        aFile - The file to get the canonical path from. May be null.
        Returns:
        null if the passed file is null or 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 not null.
        Parameters:
        aFile - The file to get the canonical path from. May be null.
        Returns:
        null if the passed file is null.
        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 not null. In case of an IOException, null is returned.
        Parameters:
        aFile - The file to get the canonical path from. May be null.
        Returns:
        null if the passed file is null.
      • 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 be null.
        aStartDirectory - The directory where the search starts. May not be null.
        Returns:
        true if the search directory is a parent of the start directory, false otherwise.
        See Also:
        getCanonicalFile(Path)
      • 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 be null.
        Returns:
        null if 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 be null.
        eAppend - Appending mode. May not be null.
        Returns:
        null if the file could not be opened
      • isFileNewer

        public static boolean isFileNewer​(@Nonnull
                                          Path aFile1,
                                          @Nonnull
                                          Path aFile2)
        Returns 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.
        Parameters:
        aFile1 - First file. May not be null.
        aFile2 - Second file. May not be null.
        Returns:
        true if the first file is newer than the second file, false otherwise.
      • 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 be null and 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,
                                        @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 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).

        Parameters:
        aStart - the starting file
        aOptions - options to configure the traversal
        nMaxDepth - the maximum number of directory levels to visit
        aVisitor - 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 for Path.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 be null.
        Returns:
        Never null.
      • getDirectoryContent

        @Nonnull
        @ReturnsMutableCopy
        public static ICommonsList<Path> getDirectoryContent​(@Nonnull
                                                             Path aDirectory,
                                                             @Nullable
                                                             Predicate<? super Path> aPathFilter)
        This is a replacement for Path.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 be null.
        aPathFilter - The path filter to be used. May not be null.
        Returns:
        Never null.