Read and write Properties files in Java

Properties class is one of the most common ways in Java to store data to the file system. Properties files are usually used to store application settings and localization data. This tutorial shows how to create a Properties file to the file system and also how to read the contents of the file back to the Java program.

Basics

The important methods for this tutorial are the Properties.store, Properties.load, Properties.put and Properties.get. The first two functions, as their names imply, handle the storing and loading of the Property file. The last two handle updating and reading data from the Properties instance.

The example below shows how to create a Properties file and read one from the file system:

Example

package com.caphal.java.properties;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;

public class PropertiesTutorial {

    private static final String FILENAME = "example.properties";

    private static final String NAME_KEY = "name";
    private static final String YEAR_KEY = "year";
    private static final String WEBSITE_KEY = "website";

    private static void saveProperties() {
        Properties props = new Properties();
        props.put(NAME_KEY, "Caphal.com");
        props.put(YEAR_KEY, ""+2014);
        props.put(WEBSITE_KEY, "http://www.caphal.com/");

        try {
            OutputStream fos = new FileOutputStream(new File(FILENAME));
            props.store(fos, "This is a header written to the properties file");
            System.out.println("Properties file written succesfully");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void loadProperties() {
        Properties props = new Properties();

        FileInputStream fis;
        try {
            fis = new FileInputStream(new File(FILENAME));
            props.load(fis);
            System.out.println("Properties file loaded succesfully");
            System.out.println(NAME_KEY+": " + props.getProperty(NAME_KEY));
            System.out.println(YEAR_KEY+": " + props.getProperty(YEAR_KEY));
            System.out.println(WEBSITE_KEY+": " + props.getProperty(WEBSITE_KEY));

        } catch (IOException e) {
            e.printStackTrace();
        }        
    }

    public static void main(String[] args) {

        saveProperties();

        loadProperties();
    }

}

The saveProperties method first creates the Properties instance and fills it with some data with the put calls. Then an OutputStream is opened to which is used by the store method to write the instance to the file system with the predefined file name. The data is stored in th file as a simple key=value syntax.

The loadProperties method on the other hand creates first an empty Properties instance. Then a FileInputStream is created, which points to the same predefined file name, which is passed to the load method for loading the contents from the file system. After reading the file, the keys are printed to the console and there you can check that they’re the same ones that were written in the saveProperties method.

One thing not shown in the example is the XML support found in the Properties class. Instead of store method you can use storeToXML to write the Properties as an XML file. And as you probably guessed you can replace load call with loadFromXML which loads the data from an XML file back to the Java code.

That’s pretty much there is to Properties files. All in all Properties is pretty simple way to store data. But if you need something more complex than key value store then Properties class is probably not the best option.

One notable thing is that Properties uses ISO 8859-1 character encoding when writing the file with the store method and expects it when reading the file with load. So if you create your properties file outside of the Java program, make sure it’s encoded properly. The storeToXML method uses UTF-8 by default when writing the xml file so with that you should avoid eny encoding issues.

Further reading

Comments

comments powered by Disqus