Skip to content

ConfigurationFileHandler

Raven Computing edited this page Mar 4, 2020 · 2 revisions

ConfigurationFileHandler

Configuration files are found in almost all applications. They usually have a .config file extension (sometimes people just write .conf). Of course, you could invent your own format but then you would also have to implement your own parser. On the other hand, if your configuration files adhere to the format descrided in this document then you can use the ConfigurationFileHandler class.

Let's take a look at the following example configuration file:

################################
#                              #
# This is a configuration file #
#                              #
################################

# Global configuration
[Global]
myKey1=42
myKey2=12345
#This is a comment
myKey3=Bla bla blub

# Local configuration
[Local]
myKey1=some text
myKey2=223

As you can see, a typical configuration file consits of multiple key-value pairs divided into various sections. Keys and values are separated by an equal sign ('=') which means that you cannot use equal signs in either the key or the value. Comments must start with a '#'. Sections devide the configuration file into logical parts. A section begins with the section name encapsulated by square brackets. Everything following the section name is considered part of that section until a new section is declared.

Read a .config file

To read a .config file from the filesystem simply construct a ConfigurationFileHandler by passing the file you want to read, either as a File object or directly as a String, to the constructor. You can then call the read() method which will return a ConfigurationFile object:

ConfigurationFile config = new ConfigurationFileHandler("myFile.config").read();

You can then query all of the configurations by first getting the section and then the value you want. For example the following code should print out the configuration value with the key named "myKey1" in the "Global" section:

System.out.println(config.getSection("Global").valueOf("mykey1"));

NOTE: You may have noticed that in the example file shown above, there are duplicate keys. This is no problem as long as they are in a different section.

You may change a specific configuration by calling the set() method on a Section. For example:

config.getSection("Local").set("myKey1", "myNewValue");

This will change the configuration value with the key "myKey1" in the "Local" section to "myNewValue". Please note that if there is no configuration entry for the specified key, it will be added to the end of that section.

Write to a .config File

You may want to persist a ConfigurationFile object to a file after you have set or added a configuration. Simply call the write() method of the ConfigurationFileHandler and pass the ConfigurationFile object you want to persist as an argument:

new ConfigurationFileHandler("myFile.config").write(config);

The entire structure and all comments are preserved when writing to a file. You could also add new sections to a configuration file

Clone this wiki locally