-
Notifications
You must be signed in to change notification settings - Fork 29
1.x Writing and reading files
FileConfig is an interface that provides the writeTo(File) and readFrom(File) methods. Actually, the configs associated with a file type: JsonConfig, HoconConfig, TomlConfig, YamlConfig, they all implement FileConfig!
It's really easy to use:
TomlConfig config = new TomlConfig();// Creates an empty TOML configuration
config.readFrom(myFile);// Reads the data from the file and put it in the config
config.writeTo(myFile);// Writes the config to the file in the TOML format
You can also specify some advanced parameters:
config.readFrom(myFile, true);// Merges the read data with the existing one
config.readFrom(myFile, false);// Replaces the existing data with the read one (this is the default behavior when no boolean is specified)
config.writeTo(myFile, true);// Appends the data to the file, ie writes it at the end without removing anything
config.writeTo(myFile, false);// Replaces the file's content with the config's data (this is the default behavior when no boolean is specified)
The ConfigParser interface provides a lot of methods to parse Readers, InputStreams, Files and URLs. Each config format module (json, hocon, toml, yaml) provides its own implementation of ConfigParser.
Example:
TomlParser parser = new TomlParser();
TomlConfig config = parser.parse(myStream);// Creates a TomlConfig with the data read from the InputStream
TomlConfig config = new TomlConfig();
//... work with the config
parser.parse(config, myStream);// Parses the data from the InputStream and add it to the config
You can of course choose some advanced parameters:
parser.setLenientWithBareKeys(true);// Enables "lenient bare keys" which makes the parser less strict about the allowed characters in bare key.
// Increases the initial capacity of various objects used to parse the data:
parser.setInitialStringBuilderCapacity(100);// Use a "big" value like this if you know that the data contains long strings
parser.setInitialListCapacity(50);// Use a "big" value like this if you know that the data contains big lists/arrays
More informations about the lenient mode here (section "lenient bare keys"). Note that it makes the parser non-compliant with the TOML specification.
The advanced parameters vary from one ConfigParser to another.
The ConfigWriter interface provides a lot of methods to parse Readers, InputStreams, Files and URLs. Each config format module (json, hocon, toml, yaml) provides its own implementation of ConfigWriter.
Example:
TomlWriter writer = new TomlWriter();
writer.write(config, myStream);// Writes the config to the OutputStream
Again, there are some advanced parameters:
writer.setLenientWithBareKeys(true);// Enables "lenient bare keys" which makes the writer less likely to put quotes around keys.
writer.setWriteTableInlinePredicate(table -> table.size() <= 2);// Writes every table containing less than 2 elements as an inline table.
writer.setIndentArrayElementsPredicate(array -> array.size() >= 5);// Indents the content of every array containing more than 5 elements
writer.setWriteStringLiteralPredicate(string -> string.contains("\'"));// Writes every String containing at least one double quote as a literal string
The advanced parameters vary from one ConfigWriter to another.