Package com.illumon.util.files
Class FileHelper
java.lang.Object
com.illumon.util.files.FileHelper
Utility/helper methods for file operations.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classGive callers ofdeleteRecursivelyOnNFS(File, Pattern)a better exception to catch. -
Method Summary
Modifier and TypeMethodDescriptionstatic voidcheckDirectory(String dir, boolean createDirectory) static voidcheckDirectory(String dir, boolean createDirectory, String message) static voidcleanDirectory(File path) Cleans the specified path.static voiddeleteRecursively(File file) Deletes a file or directory recursively.static voiddeleteRecursivelyOnNFS(File file) Recursive delete method that copes with .nfs files.static voiddeleteRecursivelyOnNFS(File trashFile, File fileToBeDeleted) Recursive delete method that copes with .nfs files.static booleandeleteRecursivelyOnNFS(File trashFile, File fileToBeDeleted, @Nullable Pattern excludePattern) AugmentdeleteRecursivelyOnNFS(File, File)with an exclude pattern.static booleandeleteRecursivelyOnNFS(File file, String excludePattern) AugmentdeleteRecursivelyOnNFS(File)with an exclude pattern.static booleandeleteRecursivelyOnNFS(File file, Pattern excludePattern) AugmentdeleteRecursivelyOnNFS(File)with an exclude pattern.static File[]findAllFiles(File dir) Scan directory recursively to find all filesstatic StringgetCanonicalForm(String path) Get the canonical path for the given path string, converting IOExceptions to UncheckIOException.static @NotNull String[]missingSafeListFilenames(File directory) Scan directory recursively to find all files, protecting against a null result.static @NotNull String[]missingSafeListFilenames(File directory, FilenameFilter filter) Scan directory recursively to find all files matching the filter, protecting against a null result.static @NotNull File[]missingSafeListFiles(File directory) Scan directory recursively to find all files, protecting against a null result.static @NotNull File[]missingSafeListFiles(File directory, FileFilter filter) Scan directory recursively to find all files matching the filter, protecting against a null result.static @NotNull File[]missingSafeListSubDirectories(File directory) Scan directory recursively to find all subdirectories, protecting against a null result.static @NotNull String[]missingSafeListSubDirectoryNames(File directory) Scan directory recursively to find all subdirectories, protecting against a null result.static voidmoveRecursively(File source, File destination, @Nullable FileFilter filter, boolean allowReplace) Move files accepted by a filter from their relative path under source to the same relative path under destination.static @NotNull StringreadTextFile(File txtFile) Read the complete contents of the named file into a string, with whitespace trimmed.static @NotNull StringreadTextFile(InputStream inputStream) Read the complete contents of the input stream into a string, with whitespace trimmed.
-
Method Details
-
getCanonicalForm
Get the canonical path for the given path string, converting IOExceptions to UncheckIOException.- Parameters:
path- The file (as String) for which to get the canonical form.- Returns:
- the canonical file string.
-
deleteRecursivelyOnNFS
AugmentdeleteRecursivelyOnNFS(File)with an exclude pattern. Files matching the pattern will not be removed, nor will containing directories.- Parameters:
file- the file or folder to delete.excludePattern- don't delete files or folders matching this pattern.- Returns:
- true if any files were excluded (so caller will know if a directory is empty)
- Throws:
FileHelper.FileDeletionException- on any errors moving/renaming/deleting files
-
deleteRecursivelyOnNFS
AugmentdeleteRecursivelyOnNFS(File)with an exclude pattern. Files matching the pattern will not be removed, nor will containing directories.- Parameters:
file- the file or folder to delete.excludePattern- don't delete files or folders matching this pattern.- Returns:
- true if any files were excluded (so caller will know if a directory is empty)
- Throws:
FileHelper.FileDeletionException- on any errors moving/renaming/deleting files
-
deleteRecursivelyOnNFS
public static boolean deleteRecursivelyOnNFS(File trashFile, File fileToBeDeleted, @Nullable @Nullable Pattern excludePattern) AugmentdeleteRecursivelyOnNFS(File, File)with an exclude pattern. Files matching the pattern will not be removed, nor will containing directories.This is an implementation method for
deleteRecursivelyOnNFS(File, String)- Parameters:
trashFile- Filename to move regular files to before deletion. .nfs files may be created in its parent directory.fileToBeDeleted- File or directory at which to begin recursive deletion.excludePattern- don't delete files or folders matching this pattern.- Returns:
- true if any files were excluded (so caller will know if a directory is empty)
- Throws:
FileHelper.FileDeletionException- on any errors moving/renaming/deleting files
-
cleanDirectory
Cleans the specified path. All files and subdirectories in the path will be deleted. (ie you'll be left with an empty directory).- Parameters:
path- The path to clean
-
deleteRecursively
Deletes a file or directory recursively.- Parameters:
file- the file or directory to delete- Throws:
FileHelper.FileDeletionException- if the file exists and could not be deleted
-
moveRecursively
public static void moveRecursively(File source, File destination, @Nullable @Nullable FileFilter filter, boolean allowReplace) Move files accepted by a filter from their relative path under source to the same relative path under destination. Creates missing destination subdirectories as needed.- Parameters:
source- Must be a directory.destination- Must be a directory if it exists.filter- Applied to normal files, only. We recurse on directories automatically.allowReplace- If the destination regular file exists, do we replace it, or silently ignore it?
-
deleteRecursivelyOnNFS
Recursive delete method that copes with .nfs files. Uses the file's parent as the trash directory.- Parameters:
file- the file or directory to delete.
-
deleteRecursivelyOnNFS
Recursive delete method that copes with .nfs files.- Parameters:
trashFile- Filename to move regular files to before deletion. .nfs files may be created in its parent directory.fileToBeDeleted- File or directory at which to begin recursive deletion.
-
findAllFiles
Scan directory recursively to find all files- Parameters:
dir- directory to scan- Returns:
- array of files located in the given directory
-
missingSafeListFiles
Scan directory recursively to find all files, protecting against a null result.- Parameters:
directory- directory to scan- Returns:
- array of files located in the given directory
-
missingSafeListFiles
Scan directory recursively to find all files matching the filter, protecting against a null result.- Parameters:
directory- directory to scanfilter- filter to apply to the files- Returns:
- array of files located in the given directory
-
missingSafeListSubDirectories
Scan directory recursively to find all subdirectories, protecting against a null result.- Parameters:
directory- directory to scan- Returns:
- array of subdirectories located in the given directory
-
missingSafeListFilenames
Scan directory recursively to find all files, protecting against a null result.- Parameters:
directory- directory to scan- Returns:
- array of files located in the given directory
-
missingSafeListFilenames
@NotNull public static @NotNull String[] missingSafeListFilenames(File directory, FilenameFilter filter) Scan directory recursively to find all files matching the filter, protecting against a null result.- Parameters:
directory- directory to scanfilter- filter to apply to the files- Returns:
- array of files located in the given directory
-
missingSafeListSubDirectoryNames
Scan directory recursively to find all subdirectories, protecting against a null result.- Parameters:
directory- directory to scan- Returns:
- array of subdirectories located in the given directory
-
readTextFile
Read the complete contents of the named file into a string, with whitespace trimmed.- Parameters:
txtFile- the file to read- Returns:
- the contents of the file as a String
- Throws:
IOException- if the file cannot be read
-
readTextFile
Read the complete contents of the input stream into a string, with whitespace trimmed.- Parameters:
inputStream- the open InputStream to read- Returns:
- the contents of the file as a String
- Throws:
IOException- if the file cannot be read
-
checkDirectory
- Throws:
IOException
-
checkDirectory
public static void checkDirectory(String dir, boolean createDirectory, String message) throws IOException - Throws:
IOException
-