Package com.github.tadukoo.util
Class FileUtil
java.lang.Object
com.github.tadukoo.util.FileUtil
Util functions for dealing with Files.
- Since:
- Pre-Alpha
- Version:
- Beta v.0.5
- Author:
- Logan Ferree (Tadukoo)
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic FilecreateDirectory(String directoryPath) Creates a directory at the given directoryPath, including any parent directories necessary, and returns theFileobject to be used.static FilecreateFile(String filepath) Creates a file at the given filepath, including any directories necessary, and returns theFileobject to be used.static booleanChecks if the givenFileexists.static booleanChecks if the file at the given filepath exists.static StringgetFileExtension(String filepath) Finds the file extension for the given filepath, if one exists.listAllFiles(File directory) Creates a List of all Files in the given directory and any of its sub-directories.listAllFiles(String directoryPath) Creates a List of all Files in the given directory and any of its sub-directories.static booleanChecks if the givenFiledoes not exist.static booleanChecks if the file at the given filepath does not exist.static byte[]readAsBytes(File file) Reads the given file into a byte array.static byte[]readAsBytes(String filepath) Reads the file at the given filepath into a byte array.static StringreadAsString(File file) Reads the givenFileas a Stringstatic StringreadAsString(String filepath) Reads the file at the given filepath as a StringreadLinesAsList(File file) Creates a List of Strings for each line in the file being read.readLinesAsList(Reader reader) Creates a List of Strings for each line in the file being read in the givenReader.readLinesAsList(String filepath) Creates a List of Strings for each line in the file being read from the given filepath.static BufferedReadersetupFileReader(File file) Creates a newBufferedReaderfor the givenFile.static BufferedReadersetupFileReader(String filepath) Creates a newBufferedReaderfor the file at the given filepath.static voidUnzips a zip file into the given destination directory.static voidUnzips a zip file into the given destination path.static voidWrites the given string to the file given in theWriter.static voidwriteFile(Writer writer, Collection<String> lines) Writes the given lines to the file given in theWriter.static voidWrites the given string to the file given by the filepath.static voidCreates a zip file using the given File (can be a file or directory).private static voidzipFile(File fileToZip, String fileName, ZipOutputStream zos) 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.static voidCreates a zip file using the file or directory at the given path.
-
Constructor Details
-
FileUtil
private FileUtil()Not allowed to create a FileUtil
-
-
Method Details
-
getFileExtension
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
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
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
Creates a file at the given filepath, including any directories necessary, and returns theFileobject 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
Creates a directory at the given directoryPath, including any parent directories necessary, and returns theFileobject to be used.- Parameters:
directoryPath- The path for the directory to be created- Returns:
- The newly created directory
-
setupFileReader
Creates a newBufferedReaderfor the file at the given filepath.- Parameters:
filepath- The path of the file to be read- Returns:
- A
BufferedReaderfor the given file - Throws:
FileNotFoundException- If the file can't be found
-
setupFileReader
Creates a newBufferedReaderfor the givenFile.- Parameters:
file- TheFileto be read- Returns:
- A
BufferedReaderfor the givenFile - Throws:
FileNotFoundException- If the file can't be found
-
exists
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
Checks if the givenFileexists.- Parameters:
file- TheFileto be checked- Returns:
- true if the file exists, false if it doesn't
-
notExists
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
Checks if the givenFiledoes not exist.- Parameters:
file- TheFileto be checked- Returns:
- false if the file exists, true if it doesn't
-
readAsString
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
Reads the givenFileas a String- Parameters:
file- TheFileto be read- Returns:
- A String representing the contents of the file
- Throws:
IOException- If something goes wrong in reading the file
-
readLinesAsList
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
Creates a List of Strings for each line in the file being read.- Parameters:
file- TheFileto read- Returns:
- A List of lines in the file
- Throws:
IOException- If something goes wrong in reading the file
-
readLinesAsList
Creates a List of Strings for each line in the file being read in the givenReader.- 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
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
Reads the given file into a byte array.- Parameters:
file- TheFileto be read- Returns:
- The byte array of the given file
- Throws:
IOException- If something goes wrong in reading the file
-
writeFile
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 tocontent- The content of the file to be written- Throws:
IOException- If something goes wrong in writing the file
-
writeFile
Writes the given string to the file given in theWriter.- Parameters:
writer- The Writer to use in writingcontent- The content of the file to be written- Throws:
IOException- If something goes wrong in writing the file
-
writeFile
Writes the given lines to the file given in theWriter.- Parameters:
writer- The Writer to use in writinglines- The content of the file to be written- Throws:
IOException- If something goes wrong in writing the file
-
zipFile
Creates a zip file using the file or directory at the given path.- Parameters:
pathToZip- The path to the file or directory to be zippedzipPath- The path (and name) of the zip file to be created- Throws:
IOException- If something goes wrong in zipping the files
-
zipFile
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 zippedzos- The ZipOutputStream to be used in zipping- Throws:
IOException- If something goes wrong in zipping the file
-
unzipFile
Unzips a zip file into the given destination path.- Parameters:
zipPath- The path to the zip filedestinationPath- The path to extract the zip file contents to- Throws:
IOException- If something goes wrong in unzipping the file
-
unzipFile
Unzips a zip file into the given destination directory.- Parameters:
zipPath- The path to the zip filedestDirectory- The directory to extract the zip file contents to- Throws:
IOException- If something goes wrong in unzipping the file
-