2. Load your config file

Scala API

Simply construct Config instances using the factory method.

import uk.co.bigbeeconsultants.bconfig.Config
val config = Config(new File("src/test/resources/sample1.txt"))

You can create a new Config instance from a File or anything that provides an InputStream. Or use the Config.fromClasspath method to read a resource from the classpath.

When constructing a Config, you can include another Config as a fallback source of default values; this too can have a fallback so a chain of any length of them is possible.

import uk.co.bigbeeconsultants.bconfig.Config
val fallback = Config.fromClasspath("resources/default.txt")
val config = Config(new File("/etc/myapp/config.txt"), fallback)

Here’s a more elaborate example that only reads the overriding config file if it is present, a commonly used pattern.

import uk.co.bigbeeconsultants.bconfig.Config
val file = new File("/etc/myapp/config.txt")
val fallback = Config.fromClasspath("resources/default.txt")
val config = if (file.canRead) Config(file, fallback) else fallback

If, instead of cascading from the fallback values, you want to override them exactly, instead pass the fallback’s keySet to the verifyKeys method, which returns the Config on which it was called.

import uk.co.bigbeeconsultants.bconfig.Config
val file = new File("/etc/myapp/config.txt")
val fallback = Config.fromClasspath("resources/default.txt")
val config = if (file.canRead) Config(file).verifyKeys(fallback.keySet) else fallback

This will throw a helpful ConfigException if the set of keys in the file is different from those in the default resources.

Parser

All the above examples actually delegate to the Parser object. But you only need to use Parser directly if the key/value separator needs to be something other than ‘=’. For example, if your configuration file uses ‘:’ instead of ‘=’ then you need

import uk.co.bigbeeconsultants.bconfig.Config
val config = Config(Parser.readFile(new File("src/test/resources/sample1.txt"), ':'))

There are other alternatives in Parser; these are documented in the API.

Java API

Simply construct ConfigJ instances using one of the static factory methods.

import uk.co.bigbeeconsultants.bconfig.ConfigJ;
...
ConfigJ config = ConfigJ.from(new File("src/test/resources/sample1.txt"));

You can create a new ConfigJ instance from a File or anything that provides an InputStream. Or use the ConfigJ.fromClasspath method to read a resource from the classpath.

When constructing a ConfigJ, you can include another ConfigJ as a fallback source of default values; this too can have a fallback so a chain of any length of them is possible.

import uk.co.bigbeeconsultants.bconfig.ConfigJ;
...
ConfigJ fallback = ConfigJ.fromClasspath("resources/default.txt");
ConfigJ config = ConfigJ.from(new File("/etc/myapp/config.txt"), fallback);

Here’s a more elaborate example that only reads the overriding config file if it is present, a commonly used pattern.

import uk.co.bigbeeconsultants.bconfig.ConfigJ;
...
File file = new File("/etc/myapp/config.txt");
ConfigJ fallback = ConfigJ.fromClasspath("resources/default.txt");
ConfigJ config = (file.canRead) ?
    ConfigJ.from(file, fallback) :
    fallback;

If, instead of cascading from the fallback values, you want to override them exactly, instead pass the fallback’s keySet to the verifyKeys method, which returns the Config on which it was called.

import uk.co.bigbeeconsultants.bconfig.ConfigJ;
...
File file = new File("/etc/myapp/config.txt");
ConfigJ fallback = ConfigJ.fromClasspath("resources/default.txt");
ConfigJ config = (file.canRead) ?
    Config(file).verifyKeys(fallback.keySet) :
    fallback

This will throw a helpful ConfigException if the set of keys in the file is different from those in the default resources.

Parser

All the above examples actually delegate to the Parser object. But you only need to use ParserJ directly if the key/value separator needs to be something other than ‘=’. For example, if your configuration file uses ‘:’ instead of ‘=’ then you need

import uk.co.bigbeeconsultants.bconfig.ConfigJ;
...
ConfigJ config = ConfigJ.from(ParserJ.readFile(new File("src/test/resources/sample1.txt"), ':'))

There are other alternatives in ParserJ; these are documented in the API.