Class FileUtils

java.lang.Object
org.pepsoft.util.FileUtils

public class FileUtils extends Object
  • Method Details

    • getMD5

      public static Checksum getMD5(File file) throws IOException
      Throws:
      IOException
    • load

      public static String load(File file, Charset charset) throws IOException
      Throws:
      IOException
    • load

      public static String load(InputStream inputStream, Charset charset) throws IOException
      Throws:
      IOException
    • copyDir

      public static void copyDir(File dir, File destDir) throws IOException
      Recursively copy a directory including all contents.
      Parameters:
      dir - The directory to copy. Must exist.
      destDir - The directory into which to copy the contents of dir. Must not exist yet and will be created.
      Throws:
      IOException - If there is an I/O error while performing the copy.
    • copyFileToFile

      public static void copyFileToFile(File file, File destFile, boolean overwrite) throws IOException
      Copy a file to another file.
      Parameters:
      file - The file to copy.
      destFile - The file to copy the file to. If overwrite is false it must not exist yet. In either case it may not be an existing directory.
      overwrite - Whether destFile should be overwritten if it already exists. If this is false and the file does already exist an IllegalStateException will be thrown.
      Throws:
      IOException - If there is an I/O error while performing the copy.
      IllegalStateException - If overwrite was false and destFile already existed.
    • copyFileToDir

      public static void copyFileToDir(File file, File destDir) throws IOException
      Copy a file to another directory.
      Parameters:
      file - The file to copy.
      destDir - The directory to copy the file into, using the same name as the source file.
      Throws:
      IOException - If there is an I/O error while performing the copy.
    • copyFileToDir

      public static void copyFileToDir(File file, File destDir, ProgressReceiver progressReceiver) throws IOException, ProgressReceiver.OperationCancelled
      Copy a file to another directory with optional progress reporting.
      Parameters:
      file - The file to copy.
      destDir - The directory to copy the file into, using the same name as the source file.
      progressReceiver - The progress receiver to report copying progress to. May be null.
      Throws:
      IOException - If there is an I/O error while performing the copy.
      ProgressReceiver.OperationCancelled
    • deleteDir

      public static boolean deleteDir(File dir)
      Recursively delete a directory and all its contents.
      Parameters:
      dir - The directory to delete.
      Returns:
      true if and only if the directory is successfully deleted; false otherwise
    • emptyDir

      public static boolean emptyDir(File dir)
      Recursively delete all contents of a directory.
      Parameters:
      dir - The directory to empty.
      Returns:
      true if and only if all contents of the directory were successfully deleted; false otherwise
    • sanitiseName

      public static String sanitiseName(@NonNls @NonNls String filename)
      Sanitises a filename by replacing characters which are illegal for Windows, Linux or Mac OS filenames with underscores and enforcing other rules.
      Parameters:
      filename - The filename to sanitise.
      Returns:
      The sanitised filename.
    • absolutise

      public static File absolutise(File file)
      Checks if a File is really a java.io.File, and if not converts it to one using File.getAbsolutePath().
      Parameters:
      file - The file to absolutise. May be null.
      Returns:
      A file with the same absolute path as the input and guaranteed to be of class java.io.File, or null if the input was null.
    • absolutise

      public static <T extends Collection<File>> T absolutise(T collection)
      Ensures that a collection of Files only contains instances of java.io.File and not subclasses, by converting subclasses using absolutise(File). The collection is transformed in-place if possible; otherwise a new collection with the same basic characteristics is created.
      Type Parameters:
      T - The type of collection.
      Parameters:
      collection - The collection to transform.
      Returns:
      Either the same collection, transformed in-place, or a new collection of the same basic type as the input containing the transformed results.
    • absolutise

      public static <T extends Map<?, ?>> T absolutise(T map)
      Ensures that a map with Files as keys and/or values only contains instances of java.io.File and not subclasses, by converting subclasses using absolutise(File). The map is transformed in-place if possible; otherwise a new map with the same basic characteristics is created.
      Type Parameters:
      T - The type of map.
      Parameters:
      map - The map to transform.
      Returns:
      Either the same map, transformed in-place, or a new map of the same basic type as the input containing the transformed results.
    • rotateFile

      public static void rotateFile(File file, String namePattern, int index, int maxIndex) throws IOException
      Recursively move a file out of the way by using increasing index numbers, renaming each file to the next number and deleting the last one.
      Parameters:
      file - The file to rotate or delete.
      namePattern - The pattern to use to create an indexed filename. Must contain a "{0}" which will be replaced with the index number.
      index - The index of the current file.
      maxIndex - The maximum index which should exist. Beyond this the file will be deleted instead of renamed.
      Throws:
      IOException - If a file could not be renamed or deleted.
    • assertEquals

      public static void assertEquals(File expected, File actual) throws IOException
      Asserts that the contents of two files or directories are equal.

      Files have to have the same length and have the exact same contents.

      Directories have to contain the same files and directories and are compared recursively.

      Parameters:
      expected - The file or directory that is expected.
      actual - The actual file or directory.
      Throws:
      AssertionError - If the specified files or directories are not equal.
      IOException - If an I/O error occurred while comparing the files or directories.
    • getTreeSize

      public static long getTreeSize(File dir)
      Calculate the total size of the files in the specified directory, recursively. Note that this method does not take the size of directories into account, nor the block or sector size of the filesystem.
      Parameters:
      dir - The directory of which to calculate the total size.
      Returns:
      The total size of the files in the specified directory and all its subdirectories, in bytes.
    • getFreeSpace

      public static long getFreeSpace(File path) throws IOException
      Determine the free space on the file system containing a specific file or directory.
      Parameters:
      path - The file or directory for which to determine its containing filesystem's free space.
      Returns:
      The free space on the specified path's filesystem in bytes.
      Throws:
      IOException - If an I/O error occurs while determining the free space.
    • stripExtension

      public static String stripExtension(String filename)
      Strip the extension off a filename, if any.
      Parameters:
      filename - The filename to strip.
      Returns:
      The filename without its extension, or the original filename if it has no extension.
    • stripDirectory

      public static String stripDirectory(String path)
      Strip the directory from a path, if any.
      Parameters:
      path - The path to strip.
      Returns:
      The path without the directory, or the original path if it was not in a directory.
    • visitFilesRecursively

      public static void visitFilesRecursively(File directory, Consumer<File> visitor) throws IOException
      Visit all files contained in a directory, recursively. The order is undefined.

      This method tries to guard against endless loops caused by symbolic or hard links creating a cycle, by keeping track of the canonical path of all visited directories.

      Parameters:
      directory - The directory to visit.
      visitor - The visitor that will be invoked for every file in the specified directory, recursively.
      Throws:
      IOException
    • main

      public static void main(String[] args) throws IOException
      Throws:
      IOException