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
-
Method Summary
Modifier and TypeMethodDescriptionstatic File
createDirectory
(String directoryPath) Creates a directory at the given directoryPath, including any parent directories necessary, and returns theFile
object to be used.static File
createFile
(String filepath) Creates a file at the given filepath, including any directories necessary, and returns theFile
object to be used.static boolean
Checks if the givenFile
exists.static boolean
Checks if the file at the given filepath exists.static String
getFileExtension
(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 boolean
Checks if the givenFile
does not exist.static boolean
Checks 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 String
readAsString
(File file) Reads the givenFile
as a Stringstatic String
readAsString
(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 BufferedReader
setupFileReader
(File file) Creates a newBufferedReader
for the givenFile
.static BufferedReader
setupFileReader
(String filepath) Creates a newBufferedReader
for the file at the given filepath.static void
Unzips a zip file into the given destination directory.static void
Unzips a zip file into the given destination path.static void
Writes the given string to the file given in theWriter
.static void
writeFile
(Writer writer, Collection<String> lines) Writes the given lines to the file given in theWriter
.static void
Writes the given string to the file given by the filepath.static void
Creates a zip file using the given File (can be a file or directory).private static void
zipFile
(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 void
Creates 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 theFile
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
Creates a directory at the given directoryPath, including any parent directories necessary, and returns theFile
object to be used.- Parameters:
directoryPath
- The path for the directory to be created- Returns:
- The newly created directory
-
setupFileReader
Creates a newBufferedReader
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
Creates a newBufferedReader
for the givenFile
.- Parameters:
file
- TheFile
to be read- Returns:
- A
BufferedReader
for 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 givenFile
exists.- Parameters:
file
- TheFile
to 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 givenFile
does not exist.- Parameters:
file
- TheFile
to 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 givenFile
as a String- Parameters:
file
- TheFile
to 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
- TheFile
to 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
- TheFile
to 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
-