Class FileUtils
java.lang.Object
com.clumd.projects.java_common_utils.files.FileUtils
A collection of useful methods to call for various File operations I find myself needing to do frequently.
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidUsed to delete a directory on the filesystem if it exists, if not - no action is taken.static voiddeleteFileIfExists(String path) Used to delete a file on the filesystem if it exists, if not - no action is taken.static voiddeleteIfExists(String path) Used to delete an item on the filesystem if it exists, if not - no action is taken.static byte[]getFileAsBytes(String path) This is used to return the raw bytes of a given file.static StringgetFileAsString(String file) This method takes a file path, and concats all the content into a single string.getFileAsStrings(String file) This method takes a file path, and returns an array of strings for each line in the file.static StringgetLocalResourceAsString(String resourceName) This is used to get a file part of the source as a string.getLocalResourceAsStrings(String resourceName) This is used to get the contents of a file line by line.static voidmakeAllDirs(String path) Create all the containing directories INCLUDING the leaf for the given path, if not exists.static voidmakeContainingDirs(String path) Create all the containing directories for the given path, if not exists.static FilevalidateIsDirectory(String path) This is used to validate the existence (as a directory) and permission to read from the given path.static FilevalidateIsFile(String path) This is used to validate the existence (as a file) and permission to read.static voidwriteBytesToFile(byte[] data, String path, boolean append) Used to write raw bytes to a file.static voidwriteStringsToFile(Collection<String> data, String path, boolean append) Used to write multiple strings to a file.static voidwriteStringToFile(String data, String path, boolean append) Used to write a single string to a file.
-
Method Details
-
getFileAsString
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
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
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
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
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
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
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
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 intoappend- 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 intoappend- 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
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 intoappend- 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
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
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
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
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
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.
-