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 class
Give callers ofdeleteRecursivelyOnNFS(File, Pattern)
a better exception to catch. -
Method Summary
Modifier and TypeMethodDescriptionstatic void
cleanDirectory
(File path) Cleans the specified path.static void
deleteRecursively
(File file) Deletes a file or directory recursively.static void
deleteRecursivelyOnNFS
(File file) Recursive delete method that copes with .nfs files.static void
deleteRecursivelyOnNFS
(File trashFile, File fileToBeDeleted) Recursive delete method that copes with .nfs files.static boolean
deleteRecursivelyOnNFS
(File trashFile, File fileToBeDeleted, Pattern excludePattern) AugmentdeleteRecursivelyOnNFS(File, File)
with an exclude pattern.static boolean
deleteRecursivelyOnNFS
(File file, String excludePattern) AugmentdeleteRecursivelyOnNFS(File)
with an exclude pattern.static boolean
deleteRecursivelyOnNFS
(File file, Pattern excludePattern) AugmentdeleteRecursivelyOnNFS(File)
with an exclude pattern.static File[]
findAllFiles
(File dir) Scan directory recursively to find all filesstatic String
getCanonicalForm
(String path) Get the canonical path for the given path string, converting IOExceptions to UncheckIOException.static String[]
missingSafeListFilenames
(File directory) Scan directory recursively to find all files, protecting against a null result.static String[]
missingSafeListFilenames
(File directory, FilenameFilter filter) Scan directory recursively to find all files matching the filter, protecting against a null result.static File[]
missingSafeListFiles
(File directory) Scan directory recursively to find all files, protecting against a null result.static File[]
missingSafeListFiles
(File directory, FileFilter filter) Scan directory recursively to find all files matching the filter, protecting against a null result.static File[]
missingSafeListSubDirectories
(File directory) Scan directory recursively to find all subdirectories, protecting against a null result.static String[]
missingSafeListSubDirectoryNames
(File directory) Scan directory recursively to find all subdirectories, protecting against a null result.static void
moveRecursively
(File source, File destination, FileFilter filter, boolean allowReplace) Move files accepted by a filter from their relative path under source to the same relative path under destination.static String
readTextFile
(File txtFile) Read the complete contents of the named file into a string, with whitespace trimmed.static String
readTextFile
(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 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 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
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
-