Class MorePaths

java.lang.Object
de.cuioss.tools.io.MorePaths

public final class MorePaths extends Object
Provides Path related utilities
Author:
Oliver Wolff
  • Field Details

  • Constructor Details

  • Method Details

    • getRealPathSafely

      public static Path getRealPathSafely(Path path)
      Tries to determine the real-path by calling Path.toRealPath(java.nio.file.LinkOption...) with no further parameter passed. In case the real path can not be resolved it will LOG at warn-level and return Path.toAbsolutePath().
      Parameters:
      path - must not be null
      Returns:
      the real-path if applicable, Path.toAbsolutePath() otherwise.
    • getRealPathSafely

      public static Path getRealPathSafely(String first, String... more)
      Tries to determine the real-path, see getRealPathSafely(Path) for details and Paths.get(String, String...) for details regarding the parameter
      Parameters:
      first - the path string or initial part of the path string
      more - additional strings to be joined to form the path string
      Returns:
      the real-path if applicable, Path.toAbsolutePath() otherwise.
    • getRealPathSafely

      public static Path getRealPathSafely(File file)
      Tries to determine the real-path, see getRealPathSafely(Path) for details
      Parameters:
      file - the Path to be looked up
      Returns:
      the real-path if applicable, Path.toAbsolutePath() otherwise.
    • 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
    • backupFile

      public static Path backupFile(Path path) throws IOException
      Backups the file, identified by the given path into the backup directory, derived with getBackupDirectoryForPath(Path). The original file attributes will be applied to the copied filed, See StandardCopyOption.COPY_ATTRIBUTES.
      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
    • 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
    • saveAndBackup

      public static void saveAndBackup(Path filePath, MorePaths.FileWriteHandler fileWriteHandler) throws IOException
      Save a file by maintaining all its attributes and permissions. Also creates a backup, see backupFile(Path).

      Usage

      PathUtils.saveAndBackup(myOriginalFilePath, targetPath -> JdomHelper.writeJdomToFile(document, targetPath));

      Parameters:
      filePath - path to the original / target file
      fileWriteHandler - do your write operation to the given file path provided by MorePaths.FileWriteHandler.write(Path).
      Throws:
      IOException - if an I/O error occurs
    • 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.