Class FileHelper

java.lang.Object
com.illumon.util.files.FileHelper

public final class FileHelper extends Object
Utility/helper methods for file operations.
  • Method Details

    • getCanonicalForm

      public static String getCanonicalForm(String path)
      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

      public static boolean deleteRecursivelyOnNFS(File file, String excludePattern)
      Augment deleteRecursivelyOnNFS(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 file, Pattern excludePattern)
      Augment deleteRecursivelyOnNFS(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)
      Augment deleteRecursivelyOnNFS(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

      public static void cleanDirectory(File path)
      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

      public static void deleteRecursively(File file)
      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

      public static void deleteRecursivelyOnNFS(File file)
      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

      public static void deleteRecursivelyOnNFS(File trashFile, File fileToBeDeleted)
      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

      public static File[] findAllFiles(File dir)
      Scan directory recursively to find all files
      Parameters:
      dir - directory to scan
      Returns:
      array of files located in the given directory
    • missingSafeListFiles

      @NotNull public static File[] missingSafeListFiles(File directory)
      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

      @NotNull public static File[] missingSafeListFiles(File directory, FileFilter filter)
      Scan directory recursively to find all files matching the filter, protecting against a null result.
      Parameters:
      directory - directory to scan
      filter - filter to apply to the files
      Returns:
      array of files located in the given directory
    • missingSafeListSubDirectories

      @NotNull public static File[] missingSafeListSubDirectories(File directory)
      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

      @NotNull public static String[] missingSafeListFilenames(File directory)
      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 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 scan
      filter - filter to apply to the files
      Returns:
      array of files located in the given directory
    • missingSafeListSubDirectoryNames

      @NotNull public static String[] missingSafeListSubDirectoryNames(File directory)
      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

      @NotNull public static String readTextFile(File txtFile) throws IOException
      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

      @NotNull public static String readTextFile(InputStream inputStream) throws IOException
      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