Skip to main content

Read-only Configs

The following settings can be used to create read-only Config objects:

Config.BuilderreadOnly()
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) and remove(String)
  • setSection(...) and removeSection(String)
  • setImport(...) and removeImport(String, String)
  • load(...) (all overloads)
  • applyMods()
  • commit() and rollback()

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:

  1. System property Config.readOnly
  2. Environment variable CONFIG_READONLY
  3. 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.