-
Notifications
You must be signed in to change notification settings - Fork 29
FileConfig basics
NightConfig offers you the powerful class FileConfig
that represents one configuration file.
Before using FileConfig, please check that you've declared all the dependencies you need.
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");
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.
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();