Skip to content

FileConfig basics

Guillaume R edited this page May 5, 2018 · 3 revisions

NightConfig offers you the powerful class FileConfig that represents one configuration file.

Modules

Before using FileConfig, please check that you've declared all the dependencies you need.

-> Modules and dependencies

Initialization

The easiest way to get an instance of FileConfig is to use the static method FileConfig.of(file):

File configFile = new File("path/to/config.json");
FileConfig config = FileConfig.of(configFile);

// Shortcut:
FileConfig config = FileConfig.of("path/to/config.json");

Reading

For now, your FileConfig is empty. To read the file, simply call load():

// Reads the config file into the FileConfig object
// By default, this replaces the content of the configuration
config.load(); 

You can also use load() to reload the configuration later.

Writing

You can request your configuration to be written with save(). By default, it only requests the writing, which is done in the background. Therefore, the save() method doesn't block.

To ensure that no data is lost, you should close() the FileConfig before stopping your program. This ensures that all writing operations have completed, and release the resources associated with the configuration (in particular, it closes the file).

// Saves in the background, returns immediately without blocking (by default - this can be changed):
config.save();

// When you don't need the configuration anymore:
config.close();

Need more features?

See FileConfigBuilder