Class FileUtils
java.lang.Object
ActiverseUtils.FileUtils
FileUtils - A utility class for common file operations such as reading, writing, appending,
checking existence, and creating directories.
This class is designed to simplify save/load systems, config management, and general file I/O.
It provides convenient methods with robust error handling and verbose comments.
- Version:
- 1.4.0
- Author:
- Knivier
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanappendToFile(String filePath, String content) Appends a string content to the end of a file.static booleanConvenience method to clear (empty) a file by overwriting with an empty string.static booleanCopies a file from source path to target path, overwriting if target exists.static booleancreateDirectories(String dirPath) Creates a directory (including parent directories) if it does not already exist.static booleandeleteFile(String filePath) Deletes a file at the given path.static booleanChecks if a file or directory exists at the given path.loadLinesFromFile(String filePath) Reads a list of strings from a file, where each line is an entry.static PropertiesloadProperties(String filePath) Reads a file containing key=value pairs into a java.util.Properties object.static booleanMoves (or renames) a file from source path to target path.readAllLines(String filePath) Reads all lines from a file into a List of Strings.static StringreadFileAsString(String filePath) Reads the entire content of a file as a single String.static booleansaveLinesToFile(String filePath, List<String> lines) Saves a list of strings to a file, overwriting the file.static booleansaveProperties(String filePath, Properties properties, String comments) Saves a java.util.Properties object to a file.static booleanWrites a string content to a file, overwriting if it exists.
-
Constructor Details
-
FileUtils
public FileUtils()
-
-
Method Details
-
writeFile
-
appendToFile
Appends a string content to the end of a file. Creates the file if it does not exist.- Parameters:
filePath- The path to the file to append to.content- The string content to append.- Returns:
- true if append succeeded, false otherwise.
-
readAllLines
-
readFileAsString
-
exists
Checks if a file or directory exists at the given path.- Parameters:
path- The path to check.- Returns:
- true if the file/directory exists, false otherwise.
-
createDirectories
Creates a directory (including parent directories) if it does not already exist.- Parameters:
dirPath- The directory path to create.- Returns:
- true if the directory was created or already exists, false if an error occurred.
-
deleteFile
Deletes a file at the given path.- Parameters:
filePath- The path to the file to delete.- Returns:
- true if file was deleted, false if file didn't exist or error occurred.
-
copyFile
Copies a file from source path to target path, overwriting if target exists.- Parameters:
sourcePath- The path of the source file.targetPath- The path where the file will be copied.- Returns:
- true if copy succeeded, false otherwise.
-
moveFile
-
saveLinesToFile
Saves a list of strings to a file, overwriting the file. Each string will be written as a separate line.- Parameters:
filePath- The file to save to.lines- The lines to save.- Returns:
- true if save succeeded, false otherwise.
-
loadLinesFromFile
-
clearFile
Convenience method to clear (empty) a file by overwriting with an empty string.- Parameters:
filePath- The file to clear.- Returns:
- true if successful, false otherwise.
-
loadProperties
Reads a file containing key=value pairs into a java.util.Properties object. Useful for configs or constants stored as properties.- Parameters:
filePath- The properties file to load.- Returns:
- Properties object loaded from file, or empty Properties if error occurs.
-
saveProperties
Saves a java.util.Properties object to a file.- Parameters:
filePath- The file to save to.properties- The Properties object to save.comments- Comments to include at the top of the properties file.- Returns:
- true if save succeeded, false otherwise.
-