Class FileManager

java.lang.Object
br.net.dd.netherwingcore.common.serialization.FileManager

public class FileManager extends Object
The FileManager class provides utility methods for reading and writing configuration data to and from files. It ensures thread-safety by synchronizing write and read operations.

This class is designed to support configuration management in a structured and reliable way using key-value pair mappings. The key-value mappings are parsed according to the specified format in the file, and there is built-in handling for text and numeric value types.

Usage:

    // Write a line to a file
    FileManager.write("exampleKey=exampleValue", Paths.get("config.txt"));

    // Read a file and retrieve key-value mappings
    Mapinvalid input: '<'Key, Value> settings = FileManager.read(Paths.get("config.txt"));
This class is final and cannot be instantiated.
  • Method Details

    • write

      public static void write(String line, Path targetFile)
      Appends a single line of text to the specified file.

      If the file does not exist, it will be created. If the file already exists, the line will be appended at the end of the file. The file is written using UTF-8 encoding.

      Parameters:
      line - The line of text to write to the file. This should not be null.
      targetFile - The path to the file where the line will be written. Must be a valid file path.
      Throws:
      NullPointerException - If line or targetFile is null.
    • read

      public static Map<Key,Value> read(Path sourceFile)
      Reads a configuration file and returns a map of key-value pairs.

      The input file is expected to have lines in the following format:

      key=value
      
      Comments (lines starting with `#`), empty lines, and lines containing `[` are ignored. Keys are wrapped in the Key class, and values are wrapped in the Value class, with type information (e.g., Type.TEXT or Type.NUMBER).
      Parameters:
      sourceFile - The path to the configuration file to be read. Must be a valid file path and exist.
      Returns:
      A map of key-value pairs parsed from the file, or null if an error occurs. Each key is of the Key type, and the corresponding value is of the Value type.