Class FileUtils

java.lang.Object
ActiverseUtils.FileUtils

public class FileUtils extends Object
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 Details

    • FileUtils

      public FileUtils()
  • Method Details

    • writeFile

      public static boolean writeFile(String filePath, String content)
      Writes a string content to a file, overwriting if it exists.
      Parameters:
      filePath - The path to the file to write.
      content - The string content to write to the file.
      Returns:
      true if writing succeeded, false otherwise.
    • appendToFile

      public static boolean appendToFile(String filePath, String content)
      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

      public static List<String> readAllLines(String filePath)
      Reads all lines from a file into a List of Strings.
      Parameters:
      filePath - The path to the file to read.
      Returns:
      List of lines if successful, empty list if file not found or error occurs.
    • readFileAsString

      public static String readFileAsString(String filePath)
      Reads the entire content of a file as a single String.
      Parameters:
      filePath - The path to the file.
      Returns:
      File content as String, or empty string if error occurs.
    • exists

      public static boolean exists(String path)
      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

      public static boolean createDirectories(String dirPath)
      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

      public static boolean deleteFile(String filePath)
      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

      public static boolean copyFile(String sourcePath, String targetPath)
      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

      public static boolean moveFile(String sourcePath, String targetPath)
      Moves (or renames) a file from source path to target path.
      Parameters:
      sourcePath - The path of the source file.
      targetPath - The new path or filename.
      Returns:
      true if move succeeded, false otherwise.
    • saveLinesToFile

      public static boolean saveLinesToFile(String filePath, List<String> lines)
      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

      public static List<String> loadLinesFromFile(String filePath)
      Reads a list of strings from a file, where each line is an entry.
      Parameters:
      filePath - The file to read.
      Returns:
      List of strings, or empty list if error occurs.
    • clearFile

      public static boolean clearFile(String filePath)
      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

      public static Properties loadProperties(String filePath)
      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

      public static boolean saveProperties(String filePath, Properties properties, String comments)
      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.