Loading config file parameters to java application

Loading parameters or static contents into a program is easily handled in Java through the Properties class.

Create an instance of the Properties class and one instance of FileInputStream class that points to the configuration file.

The actual loading of the parameters are done through the method load() on the Properties object.

In the example below config parameters are read from the configuration file and then printed out.

This is what the sample configuration file (config/config.txt) looks like:

# Application configuration
# Use '#' to write any comments in config file



import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;

 * ConfigExample.java
 * @author www.codes.ssingh.in

public class ConfigExample{
    Properties config;
     * Loads configuration parameters from a textfile and print them out.
    public void loadConfigFile() {

        //Load configuration file
        String filename = "confing/config.txt";
        config = new Properties();

        try {

            config.load(new FileInputStream(filename));

        } catch (FileNotFoundException ex) {
        } catch (IOException ex) {
        //Print out the configuration parameters
        Enumeration en = config.keys();
        System.out.println("Application configuration: ");
        while (en.hasMoreElements()) {
            String key = (String) en.nextElement();
            System.out.println(key + " -> " + config.get(key));
     * Starts the program
     * @param args the command line arguments
    public static void main(String[] args) {
        new ConfigExample().loadConfigFile();

No responses yet

Leave a Reply