Raxkin Raxkin -4 years ago 101
Scala Question

Read line from file and load it to actual config

I'm using the ConfigFactory class to load the config of my application, but i want to know how can I load a line from other file and add it to the Config object.
The file only contains 1 line with the value I want, and i want to add that value with to the config with a new key or better to a existing one, so i can have a default value.

Is it possible to add a custom key/value after the config is loaded?, like:

config.add("key", "value")

Answer Source

You can do this by using #withFallback:

// Assuming this is your first config file
val default = ConfigFactory.load("application.conf")

// Now we add the second one
val updated = default.withFallback(ConfigFactory.load("foo.conf"))

Or, assuming you have read your line elsewhere as a value, use the same method but parse the config from the string directly using ConfigFactory#parseString:

val default = ConfigFactory.load("application.conf")
val updated = default.withFallback(ConfigFactory.parseString(s"key = $value"))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download