Class FileUtil

java.lang.Object
com.github.tadukoo.util.FileUtil

public final class FileUtil extends Object
Util functions for dealing with Files.
Since:
Pre-Alpha
Version:
Beta v.0.5
Author:
Logan Ferree (Tadukoo)
  • Constructor Details

    • FileUtil

      private FileUtil()
      Not allowed to create a FileUtil
  • Method Details

    • getFileExtension

      public static String getFileExtension(String filepath)
      Finds the file extension for the given filepath, if one exists.
      Parameters:
      filepath - The filepath to grab the extension for
      Returns:
      The found file extension, or null
    • listAllFiles

      public static List<File> listAllFiles(String directoryPath) throws IOException
      Creates a List of all Files in the given directory and any of its sub-directories.
      Parameters:
      directoryPath - The path to the directory to check
      Returns:
      A List of all Files in the directory and its sub-directories
      Throws:
      IOException - If something goes wrong in listing the files
    • listAllFiles

      public static List<File> listAllFiles(File directory) throws IOException
      Creates a List of all Files in the given directory and any of its sub-directories.
      Parameters:
      directory - The directory (as a File) to check
      Returns:
      A List of all Files in the directory and its sub-directories
      Throws:
      IOException - If something goes wrong in listing the files
    • createFile

      public static File createFile(String filepath) throws IOException
      Creates a file at the given filepath, including any directories necessary, and returns the File object to be used.
      Parameters:
      filepath - The path for the File to be created
      Returns:
      The newly created File
      Throws:
      IOException - If something goes wrong in creating the file
    • createDirectory

      public static File createDirectory(String directoryPath)
      Creates a directory at the given directoryPath, including any parent directories necessary, and returns the File object to be used.
      Parameters:
      directoryPath - The path for the directory to be created
      Returns:
      The newly created directory
    • setupFileReader

      public static BufferedReader setupFileReader(String filepath) throws FileNotFoundException
      Creates a new BufferedReader for the file at the given filepath.
      Parameters:
      filepath - The path of the file to be read
      Returns:
      A BufferedReader for the given file
      Throws:
      FileNotFoundException - If the file can't be found
    • setupFileReader

      public static BufferedReader setupFileReader(File file) throws FileNotFoundException
      Creates a new BufferedReader for the given File.
      Parameters:
      file - The File to be read
      Returns:
      A BufferedReader for the given File
      Throws:
      FileNotFoundException - If the file can't be found
    • exists

      public static boolean exists(String filepath)
      Checks if the file at the given filepath exists.
      Parameters:
      filepath - The path of the file to be checked
      Returns:
      true if the file exists, false if it doesn't
    • exists

      public static boolean exists(File file)
      Checks if the given File exists.
      Parameters:
      file - The File to be checked
      Returns:
      true if the file exists, false if it doesn't
    • notExists

      public static boolean notExists(String filepath)
      Checks if the file at the given filepath does not exist.
      Parameters:
      filepath - The path of the file to be checked
      Returns:
      false if the file exists, true if it doesn't
    • notExists

      public static boolean notExists(File file)
      Checks if the given File does not exist.
      Parameters:
      file - The File to be checked
      Returns:
      false if the file exists, true if it doesn't
    • readAsString

      public static String readAsString(String filepath) throws IOException
      Reads the file at the given filepath as a String
      Parameters:
      filepath - The path of the file to be read
      Returns:
      A String representing the contents of the file
      Throws:
      IOException - If something goes wrong in reading the file
    • readAsString

      public static String readAsString(File file) throws IOException
      Reads the given File as a String
      Parameters:
      file - The File to be read
      Returns:
      A String representing the contents of the file
      Throws:
      IOException - If something goes wrong in reading the file
    • readLinesAsList

      public static List<String> readLinesAsList(String filepath) throws IOException
      Creates a List of Strings for each line in the file being read from the given filepath.
      Parameters:
      filepath - The path to the file to be read
      Returns:
      A List of lines in the file
      Throws:
      IOException - If something goes wrong in reading the file
    • readLinesAsList

      public static List<String> readLinesAsList(File file) throws IOException
      Creates a List of Strings for each line in the file being read.
      Parameters:
      file - The File to read
      Returns:
      A List of lines in the file
      Throws:
      IOException - If something goes wrong in reading the file
    • readLinesAsList

      public static List<String> readLinesAsList(Reader reader) throws IOException
      Creates a List of Strings for each line in the file being read in the given Reader.
      Parameters:
      reader - The Reader to use in reading
      Returns:
      A List of lines in the file
      Throws:
      IOException - If something goes wrong in reading the file
    • readAsBytes

      public static byte[] readAsBytes(String filepath) throws IOException
      Reads the file at the given filepath into a byte array.
      Parameters:
      filepath - The path of the file to be read
      Returns:
      The byte array of the given file
      Throws:
      IOException - If something goes wrong in reading the file
    • readAsBytes

      public static byte[] readAsBytes(File file) throws IOException
      Reads the given file into a byte array.
      Parameters:
      file - The File to be read
      Returns:
      The byte array of the given file
      Throws:
      IOException - If something goes wrong in reading the file
    • writeFile

      public static void writeFile(String filepath, String content) throws IOException
      Writes the given string to the file given by the filepath. Will create the file and its directories if they don't exist.
      Parameters:
      filepath - The path to save the file to
      content - The content of the file to be written
      Throws:
      IOException - If something goes wrong in writing the file
    • writeFile

      public static void writeFile(Writer writer, String content) throws IOException
      Writes the given string to the file given in the Writer.
      Parameters:
      writer - The Writer to use in writing
      content - The content of the file to be written
      Throws:
      IOException - If something goes wrong in writing the file
    • writeFile

      public static void writeFile(Writer writer, Collection<String> lines) throws IOException
      Writes the given lines to the file given in the Writer.
      Parameters:
      writer - The Writer to use in writing
      lines - The content of the file to be written
      Throws:
      IOException - If something goes wrong in writing the file
    • zipFile

      public static void zipFile(String pathToZip, String zipPath) throws IOException
      Creates a zip file using the file or directory at the given path.
      Parameters:
      pathToZip - The path to the file or directory to be zipped
      zipPath - The path (and name) of the zip file to be created
      Throws:
      IOException - If something goes wrong in zipping the files
    • zipFile

      public static void zipFile(File fileToZip, String zipPath) throws IOException
      Creates a zip file using the given File (can be a file or directory).
      Parameters:
      fileToZip - The File to be zipped (can be file or directory)
      zipPath - The path (and name) of the zip file to be created
      Throws:
      IOException - If something goes wrong in zipping the file
    • zipFile

      private static void zipFile(File fileToZip, String fileName, ZipOutputStream zos) throws IOException
      Creates a zip file using the given File (can be a file or directory), fileName for the file to be zipped, and a ZipOutputStream to be used. Each call to this method creates a single entry in the zip file, and if it's a directory, it will make recursive calls for all children of the directory.
      Parameters:
      fileToZip - The File to be zipped (can be file or directory)
      fileName - The name of the file to be zipped
      zos - The ZipOutputStream to be used in zipping
      Throws:
      IOException - If something goes wrong in zipping the file
    • unzipFile

      public static void unzipFile(String zipPath, String destinationPath) throws IOException
      Unzips a zip file into the given destination path.
      Parameters:
      zipPath - The path to the zip file
      destinationPath - The path to extract the zip file contents to
      Throws:
      IOException - If something goes wrong in unzipping the file
    • unzipFile

      public static void unzipFile(String zipPath, File destDirectory) throws IOException
      Unzips a zip file into the given destination directory.
      Parameters:
      zipPath - The path to the zip file
      destDirectory - The directory to extract the zip file contents to
      Throws:
      IOException - If something goes wrong in unzipping the file