Skip to content

FileConfigBuilder

Guillaume Raffin edited this page Jun 14, 2023 · 7 revisions

The simplest way to get a FileConfig is to use FileConfig.of(input), but it's not really customizable. You can achieve much more with the FileConfigBuilder!

Using FileConfigBuilder

Create a new FileConfigBuilder, configure it as you see fit and build() your FileConfig! Example:

FileConfigBuilder builder = FileConfig
    .builder("path/to/config.json")
    .defaultResource("/path/in/jar/to/default_config.json")
    .sync();
FileConfig config = builder.build();

// Your config is now ready to use! For instance, you can load its contents as usual:
config.load();

Available settings

Builder method Description Default
charset Sets the charset used to read and write the file UTF-8
writingMode Sets the WritingMode used when saving the config REPLACE
parsingMode Sets the ParsingMode used to read and write the file REPLACE
onFileNotFound Defines what to do when the file doesn't exist CREATE_EMPTY
defaultResource When the file doesn't exist, copy the given resource (in the application's jar) to it Not set
defaultData(file) When the file doesn't exist, copy the given file to it Not set
defaultData(url) When the file doesn't exist, copy the given distant resource to it Not set
sync Enables synchronous write: save() will block until the data is written to the file Disabled
autosave Enables automatic saving: save() will be called after each modification Disabled
autoreload Enables automatic reloading: load() will be called after each (external) modification of the file Disabled
concurrent Makes the FileConfig thread-safe Not set

Beware of resource paths

⚠️ With defaultResource, you should always use absolute paths (like in the example above) to prevent issues such as #128.

If you want to use a relative path, call getResource on your ClassLoader and use defaultData or onFileNotFound.