Class MorePaths


  • public final class MorePaths
    extends Object
    Provides Path related utilities
    Author:
    Oliver Wolff
    • Method Detail

      • checkAccessiblePath

        public static boolean checkAccessiblePath​(@NonNull
                                                  @NonNull Path path,
                                                  boolean checkForDirectory,
                                                  boolean verbose)
        Checks whether the given Path denotes an existing read and writable directory or file.
        Parameters:
        path - to checked, must not be null
        checkForDirectory - check whether it is a file or directory
        verbose - indicates whether to log errors at warn-level
        Returns:
        boolean indicating whether the given Path denotes an existing read and writable directory.
      • checkReadablePath

        public static boolean checkReadablePath​(@NonNull
                                                @NonNull Path path,
                                                boolean checkForDirectory,
                                                boolean verbose)
        Checks whether the given Path denotes an existing readable directory or file.
        Parameters:
        path - to checked, must not be null
        checkForDirectory - check whether it is a file or directory
        verbose - indicates whether to log errors at warn-level
        Returns:
        boolean indicating whether the given Path denotes an existing readable directory.
      • checkExecutablePath

        public static boolean checkExecutablePath​(@NonNull
                                                  @NonNull Path path,
                                                  boolean verbose)
        Checks whether the given Path denotes an existing executable file.
        Parameters:
        path - to checked, must not be null
        verbose - indicates whether to log errors at warn-level
        Returns:
        boolean indicating whether the given Path denotes an existing readable directory.
      • getBackupDirectoryForPath

        public static Path getBackupDirectoryForPath​(Path directory)
        Creates / or references a backup-directory named ".backup" within the given directory and returns it
        Parameters:
        directory - must not null and denote an existing writable directory, otherwise am IllegalArgumentException will be thrown.
        Returns:
        the ".backup" directory
      • copyToTempLocation

        public static Path copyToTempLocation​(Path path)
                                       throws IOException
        Creates a temp-copy of the given file, identified by the given path. The original file attributes will be applied to the copied filed, See StandardCopyOption.COPY_ATTRIBUTES.

        Caution: Security-Impact

        Creating a temp-file might introduce a security issue. Never ever use this location for sensitive information that might be of interest for an attacker
        Parameters:
        path - must not be null and denote an existing read and writable file
        Returns:
        Path on the newly created file
        Throws:
        IOException - if an I/O error occurs
      • assertAccessibleFile

        public static void assertAccessibleFile​(Path path)
        Asserts whether a given Path is accessible, saying it exits as a file and is read and writable. If not it will throw an IllegalArgumentException
        Parameters:
        path - to be checked, must not be null
      • deleteQuietly

        public static boolean deleteQuietly​(Path path)
        Deletes a file, never throwing an exception. If file is a directory, delete it and all subdirectories. Inspired by org.apache.commons.io.FileUtils#deleteQuietly

        The difference between File.delete() and this method are:

        • A directory to be deleted does not have to be empty.
        • No exceptions are thrown when a file or directory cannot be deleted.
        Parameters:
        path - file or directory to delete, can be null
        Returns:
        true if the file or directory was deleted, otherwise false
      • contentEquals

        public static boolean contentEquals​(Path path1,
                                            Path path2)
                                     throws IOException
        Compares the contents of two files to determine if they are equal or not.

        This method checks to see if the two files are different lengths or if they point to the same file, before resorting to byte-by-byte comparison of the contents.

        Taken from org.apache.commons.io.FileUtils.contentEquals(File, File) Code origin: Avalon

        Parameters:
        path1 - the first file
        path2 - the second file
        Returns:
        true if the content of the files are equal or they both don't exist, false otherwise
        Throws:
        IOException - in case of an I/O error
      • isSameFile

        public static boolean isSameFile​(Path path,
                                         Path path2)
        Checks, if the two given paths are pointing to the same location.

        If both paths are not null and do File.exists(), the Files.isSameFile(Path, Path) method is used to check if both paths are pointing to the same location. Otherwise, if one of the paths does not exist, the Object.equals(Object) method is used.

        Parameters:
        path - to be compared with path2
        path2 - to be compared with path
        Returns:
        true, if both paths are null. true, if both paths not null, do exist, and Files.isSameFile(Path, Path). true, if both paths not null and Object.equals(Object) false otherwise.