Read-only Configs
The following settings can be used to create read-only Config objects:
Example
// Create a read-only config
Config config = Config.create("MyConfig.cfg").readOnly().build();
This causes all methods that make modifications to throw UnsupportedOperationException.
Affected methods
The following Config methods throw UnsupportedOperationException ("Cannot call this method on a read-only configuration.") on a read-only config:
set(...)(all overloads) andremove(String)setSection(...)andremoveSection(String)setImport(...)andremoveImport(String, String)load(...)(all overloads)applyMods()commit()androllback()
Read methods such as get(String), getString(String), getSection(String), writeTo(Writer) and toString() work normally.
Example
Config config = Config.create("MyConfig.cfg").readOnly().build();
String name = config.getString("General/name"); // OK
try {
config.set("General/name", "new value"); // Throws
} catch (UnsupportedOperationException e) {
// Expected.
}
Default
If readOnly() is not called, the default is the first value found:
- System property
Config.readOnly - Environment variable
CONFIG_READONLY false
Read-only applies to the Config object only.
If the store reports an external change to the underlying file (for example a FileStore with the watcher enabled; see FileStore),
the config still picks it up.