java.lang.Object
com.clumd.projects.java_common_utils.files.FileUtils

public class FileUtils extends Object
A collection of useful methods to call for various File operations I find myself needing to do frequently.
  • Method Details

    • getFileAsString

      public static String getFileAsString(String file) throws IOException
      This method takes a file path, and concats all the content into a single string.
      Parameters:
      file - The file path for the file to turn into a string
      Returns:
      The file as a single string.
      Throws:
      IOException - Thrown if it could not find, or you don't have permissions for that file.
    • getFileAsStrings

      public static List<String> getFileAsStrings(String file) throws IOException
      This method takes a file path, and returns an array of strings for each line in the file.
      Parameters:
      file - The file path for the file to turn into string array
      Returns:
      An element for each line in the file.
      Throws:
      IOException - Thrown if it could not find, or you don't have permissions for that file.
    • getFileAsBytes

      public static byte[] getFileAsBytes(String path) throws IOException
      This is used to return the raw bytes of a given file.
      Parameters:
      path - The full path to the file to get the bytes from.
      Returns:
      The byte[] of the entire file contents.
      Throws:
      IOException - Thrown if there was a problem accessing the requested file.
    • getLocalResourceAsString

      public static String getLocalResourceAsString(String resourceName) throws IOException
      This is used to get a file part of the source as a string.
      Parameters:
      resourceName - The name of the resource to get as a string
      Returns:
      The string value of the resource.
      Throws:
      IOException - Thrown if there was a problem accessing the requested file
    • getLocalResourceAsStrings

      public static List<String> getLocalResourceAsStrings(String resourceName) throws IOException
      This is used to get the contents of a file line by line.
      Parameters:
      resourceName - The name of the resource to get as a String array.
      Returns:
      The line by line values of the resource.
      Throws:
      IOException - Thrown if there was a problem accessing the requested file
    • validateIsFile

      public static File validateIsFile(String path) throws IOException
      This is used to validate the existence (as a file) and permission to read.
      Parameters:
      path - The path to validate.
      Returns:
      The String of the resolved path to that file (removal of . and .. if necessary)
      Throws:
      IOException - Thrown if there was a problem accessing that path as a file.
    • validateIsDirectory

      public static File validateIsDirectory(String path) throws IOException
      This is used to validate the existence (as a directory) and permission to read from the given path.
      Parameters:
      path - The path to validate.
      Returns:
      The String of the resolved path to that directory (removal of . and .. if necessary)
      Throws:
      IOException - Thrown if there was a problem accessing that path as a directory.
    • writeStringToFile

      public static void writeStringToFile(String data, String path, boolean append) throws IOException
      Used to write a single string to a file.
      Parameters:
      data - The String to be written to the file.
      path - The path of the file that we would like to write into
      append - If we should add to the end of the file, or overwrite from the beginning.
      Throws:
      IOException - Thrown if there was a problem writing to the file.
    • writeStringsToFile

      public static void writeStringsToFile(Collection<String> data, String path, boolean append) throws IOException
      Used to write multiple strings to a file.
      Parameters:
      data - The collection of Strings to be written to the file.
      path - The path of the file that we would like to write into
      append - If we should add to the end of the file, or overwrite from the beginning.
      Throws:
      IOException - Thrown if there was a problem writing to the file.
    • writeBytesToFile

      public static void writeBytesToFile(byte[] data, String path, boolean append) throws IOException
      Used to write raw bytes to a file.
      Parameters:
      data - The data to be written to the file.
      path - The path of the file that we would like to write into
      append - If we should add to the end of the file, or overwrite from the beginning.
      Throws:
      IOException - Thrown if there was a problem writing to the file.
    • makeContainingDirs

      public static void makeContainingDirs(String path) throws IOException
      Create all the containing directories for the given path, if not exists.

      This is a simple pass-through method.

      Parameters:
      path - The path to create the parent directories to
      Throws:
      IOException - Thrown if there was a problem creating the parent directories.
    • makeAllDirs

      public static void makeAllDirs(String path) throws IOException
      Create all the containing directories INCLUDING the leaf for the given path, if not exists.

      This is a simple pass-through method.

      Parameters:
      path - The path to create the parent directories to
      Throws:
      IOException - Thrown if there was a problem creating the parent directories.
    • deleteFileIfExists

      public static void deleteFileIfExists(String path) throws IOException
      Used to delete a file on the filesystem if it exists, if not - no action is taken.
      Parameters:
      path - The path of the file to be deleted.
      Throws:
      IOException - Thrown if there was a problem deleting the file.
    • deleteDirectoryIfExists

      public static void deleteDirectoryIfExists(String path) throws IOException
      Used to delete a directory on the filesystem if it exists, if not - no action is taken.
      Parameters:
      path - The path of the directory to be deleted.
      Throws:
      IOException - Thrown if there was a problem deleting the directory.
    • deleteIfExists

      public static void deleteIfExists(String path) throws IOException
      Used to delete an item on the filesystem if it exists, if not - no action is taken.
      Parameters:
      path - The path of the item to be deleted.
      Throws:
      IOException - Thrown if there was a problem deleting the file/directory.