Class FileUtils

java.lang.Object
nl.colorize.util.FileUtils

public final class FileUtils extends Object
Utility class for loading data from and saving data to files. Unless stated otherwise, all methods in this class will throw an IOException if an I/O error occurs while interacting with the file.

When using Java 7 or newer, some of the convenience methods in this class are no longer needed and can be replaced with Files.

  • Method Details

    • readFirstLines

      public static List<String> readFirstLines(File source, Charset encoding, int n) throws IOException
      Reads a file's first N lines, or all lines if the file contains less.
      Throws:
      IOException
    • write

      public static void write(byte[] data, File dest) throws IOException
      Writes the specified data to a file. If the file already exists its contents will be replaced.
      Throws:
      IOException
    • write

      public static void write(String text, Charset encoding, File dest) throws IOException
      Writes the specified text to a file. If the file already exists its contents will be replaced.
      Throws:
      IOException
    • write

      public static void write(List<String> lines, Charset encoding, File dest) throws IOException
      Writes the specified lines to a file. If the file already exists its contents will be replaced.
      Throws:
      IOException
    • rewrite

      public static void rewrite(File file, Charset charset, Function<String,String> lineProcessor) throws IOException
      Reads all lines in a file, then used the provided callback function to rewrite the lines. The result is then used to overwrite the original file.
      Throws:
      IOException
    • rewriteLine

      public static void rewriteLine(File file, String original, String replacement, Charset charset) throws IOException
      Reads all lines in a file, then replaced all matching lines with the specified replacement. The result is then used to overwrite the original file.
      Throws:
      IOException
    • expandUser

      public static File expandUser(String path)
      Expands a file path that contains a reference to the user's home directory. For example, if the path is "~/Desktop/test.txt" and the user home directory is "/home/john", the returned file will reference "/home/john/Desktop/test.txt".
      Throws:
      IllegalArgumentException - if the path is empty.
      UnsupportedOperationException - if the current platform does not provide or support a user home directory.
    • mkdir

      public static void mkdir(File dir) throws IOException
      Creates a directory. Unlike File.mkdir(), an exception will be thrown if the directory could not be created. If the directory already exists this method does nothing.
      Throws:
      IOException
    • mkdir

      public static File mkdir(File parentDir, String name) throws IOException
      Creates a new directory with the specified name and location, then returns the directory that was just created.
      Throws:
      IOException
    • delete

      public static void delete(File file) throws IOException
      Deletes a file. Unlike File.delete(), an exception will be thrown if the file could not be deleted. If the file does not exist this method does nothing.
      Throws:
      IOException
    • copyDirectory

      public static void copyDirectory(File source, File target) throws IOException
      Copies a directory to the specified location.
      Throws:
      IllegalStateException - if the target directory already exists.
      IOException - if an I/O error occurs while copying the files.
    • deleteDirectory

      public static void deleteDirectory(File dir) throws IOException
      Deletes a directory and all of its contents. If the directory does not exist this method does nothing.
      Throws:
      IOException - if an I/O error occurs while deleting.
      IllegalArgumentException - if the argument is not a directory.
    • walkFiles

      public static List<File> walkFiles(File dir, Predicate<File> filter) throws IOException
      Shorthand for Files.walk to iterate over files in a directory, with only files matching the filter being returned.
      Throws:
      IOException
    • getRelativePath

      @Deprecated public static String getRelativePath(File file, File base)
      Deprecated.
      Use Path.relativize instead. This can be used even when working with APIs still using File instances: base.toPath().relativize(file.toPath).
      Returns file's path relative to base. For example, the path "/a/b/c/d.txt" relative to "/a/b" would return "c/d.txt". If both paths do not share a common base file's absolute path will be returned.
    • getFileExtension

      public static String getFileExtension(File file)
      Returns the file extension of the specified file, excluding the dot. So the file "test.png" would return "png". Note that the file extension will be returned as lowercase, even if the original file name was not. If the file does not have a file extension this will return an aempty string.
    • getFileExtensionFilter

      public static FilenameFilter getFileExtensionFilter(String... extensions)
      Returns a file filter that will only accept files that match one of the specified file extensions. The file extension check is case-insensitive.
      Throws:
      IllegalArgumentException - if the provided list is empty.
    • countDirectorySize

      public static long countDirectorySize(File dir) throws IOException
      Returns the size of the specified directory, in bytes.
      Throws:
      IOException
    • formatFileSize

      public static String formatFileSize(File file)
      Returns a human-readable description of the specified file's size. The level of precision is automatically selected based on the value.
    • formatFileSize

      public static String formatFileSize(long fileSize)
      Returns a human-readable description of the specified file size in bytes. The level of precision is automatically selected based on the value.
    • createTempFile

      public static File createTempFile(byte[] data) throws IOException
      Creates a temporary file with the specified contents. The file will be created in the platform's default location for storing temporary files.
      Throws:
      IOException
    • createTempFile

      public static File createTempFile(String text, Charset encoding) throws IOException
      Creates a temporary file with the specified text as contents. The file will be created in the platform's default location for storing temporary files.
      Throws:
      IOException
    • createTempDir

      public static File createTempDir() throws IOException
      Creates a temporary directory within the platform's default location for storing temporary files.
      Throws:
      IOException
    • createTempDir

      public static File createTempDir(String name) throws IOException
      Creates a temporary directory with the specified name. The directory will be created in the platform's default location for storing temporary files.
      Throws:
      IOException