Package io.cui.tools.io
Class MorePaths
- java.lang.Object
-
- io.cui.tools.io.MorePaths
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceMorePaths.FileWriteHandlerCommand pattern interface delegating the file write operation to its caller.
-
Field Summary
Fields Modifier and Type Field Description static StringBACKUP_DIR_NAME".backup"static StringBACKUP_FILE_SUFFIXThe prefix to be attached to a backup-filestatic StringMSG_DIRECTORY_NOT_ACCESSIBLE"File or Directory {} is not accessible, reason: {}".
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidassertAccessibleFile(Path path)Asserts whether a givenPathis accessible, saying it exits as a file and is read and writable.static PathbackupFile(Path path)Backups the file, identified by the given path into the backup directory, derived withgetBackupDirectoryForPath(Path).static booleancheckAccessiblePath(@NonNull Path path, boolean checkForDirectory, boolean verbose)Checks whether the givenPathdenotes an existing read and writable directory or file.static booleancheckExecutablePath(@NonNull Path path, boolean verbose)Checks whether the givenPathdenotes an existing executable file.static booleancheckReadablePath(@NonNull Path path, boolean checkForDirectory, boolean verbose)Checks whether the givenPathdenotes an existing readable directory or file.static booleancontentEquals(Path path1, Path path2)Compares the contents of two files to determine if they are equal or not.static PathcopyToTempLocation(Path path)Creates a temp-copy of the given file, identified by the given path.static booleandeleteQuietly(Path path)Deletes a file, never throwing an exception.static PathgetBackupDirectoryForPath(Path directory)Creates / or references a backup-directory named ".backup" within the given directory and returns itstatic PathgetRealPathSafely(File file)Tries to determine the real-path, seegetRealPathSafely(Path)for detailsstatic PathgetRealPathSafely(String first, String... more)Tries to determine the real-path, seegetRealPathSafely(Path)for details andPaths.get(String, String...)for details regarding the parameterstatic PathgetRealPathSafely(Path path)Tries to determine the real-path by callingPath.toRealPath(java.nio.file.LinkOption...)with no further parameter passed.static booleanisSameFile(Path path, Path path2)Checks, if the two given paths are pointing to the same location.static voidsaveAndBackup(Path filePath, MorePaths.FileWriteHandler fileWriteHandler)Save a file by maintaining all its attributes and permissions.
-
-
-
Field Detail
-
BACKUP_DIR_NAME
public static final String BACKUP_DIR_NAME
".backup"- See Also:
- Constant Field Values
-
MSG_DIRECTORY_NOT_ACCESSIBLE
public static final String MSG_DIRECTORY_NOT_ACCESSIBLE
"File or Directory {} is not accessible, reason: {}".- See Also:
- Constant Field Values
-
BACKUP_FILE_SUFFIX
public static final String BACKUP_FILE_SUFFIX
The prefix to be attached to a backup-file- See Also:
- Constant Field Values
-
-
Method Detail
-
getRealPathSafely
public static Path getRealPathSafely(Path path)
Tries to determine the real-path by callingPath.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 returnPath.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, seegetRealPathSafely(Path)for details andPaths.get(String, String...)for details regarding the parameter- Parameters:
first- the path string or initial part of the path stringmore- 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, seegetRealPathSafely(Path)for details- Parameters:
file- thePathto 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 givenPathdenotes an existing read and writable directory or file.- Parameters:
path- to checked, must not be nullcheckForDirectory- check whether it is a file or directoryverbose- indicates whether to log errors at warn-level- Returns:
- boolean indicating whether the given
Pathdenotes an existing read and writable directory.
-
checkReadablePath
public static boolean checkReadablePath(@NonNull @NonNull Path path, boolean checkForDirectory, boolean verbose)
Checks whether the givenPathdenotes an existing readable directory or file.- Parameters:
path- to checked, must not be nullcheckForDirectory- check whether it is a file or directoryverbose- indicates whether to log errors at warn-level- Returns:
- boolean indicating whether the given
Pathdenotes an existing readable directory.
-
checkExecutablePath
public static boolean checkExecutablePath(@NonNull @NonNull Path path, boolean verbose)
Checks whether the givenPathdenotes an existing executable file.- Parameters:
path- to checked, must not be nullverbose- indicates whether to log errors at warn-level- Returns:
- boolean indicating whether the given
Pathdenotes 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 amIllegalArgumentExceptionwill 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 withgetBackupDirectoryForPath(Path). The original file attributes will be applied to the copied filed, SeeStandardCopyOption.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, SeeStandardCopyOption.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 givenPathis accessible, saying it exits as a file and is read and writable. If not it will throw anIllegalArgumentException- 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#deleteQuietlyThe 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 benull- Returns:
trueif the file or directory was deleted, otherwisefalse
-
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 filepath2- 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 filefileWriteHandler- 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
nulland doFile.exists(), theFiles.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, theObject.equals(Object)method is used.- Parameters:
path- to be compared with path2path2- to be compared with path- Returns:
true, if both paths arenull.true, if both paths notnull, do exist, andFiles.isSameFile(Path, Path).true, if both paths notnullandObject.equals(Object)falseotherwise.
-
-