Tech and Media Labs
This site uses cookies to improve the user experience.


Java Properties

Jakob Jenkov
Last update: 2019-07-05

The JavaProperties class, java.util.Properties, is like a Java Map of Java String key and value pairs. The Java Properties class can write the key, value pairs to a properties file on disk, and read the properties back in again. This is an often used mechanism for storing simple configuration properties for Java applications.

Create a Properties Instance

To use the Java Properties class you must first create a Properties instance. You do so via its constructor and the Java new instruction. Here is an example of creating a Java Properties instance:

Properties properties = new Properties();

Set Properties

To set properties in a Java Properties instance you use the setProperty() method. Here is an example of setting a property (key - value pair) in a Java Properties object:

properties.setProperty("email", "john@doe.com");

This example sets the property with the key email to the value john@doe.com.

Get Properties

To get properties from a Java Properties object you use the getProperty() method, passing the key of the property to get as parameter. Here is an example of getting a property from a Java Properties instance:

String email = properties.getProperty("email");

Store Properties to File

You can store the property key, value pairs to a properties file which can be read again later on. You store the contents of a Properties object via its store() method. Here is an example of storing the contents of a Java Properties to a properties file:

Properties properties = new Properties();

properties.setProperty("property1", "value1");
properties.setProperty("property2", "value2");
properties.setProperty("property3", "value3");

try(FileWriter output = new FileWriter("data/props.properties")){
    properties.store(output, "These are properties");
} catch (IOException e) {
    e.printStackTrace();
}

Property File Format

A Java Properties property file consists of lines with one key=value pair on each line. Here is an example Java Properties property file:

#These are properties
#Thu Jul 04 21:29:20 CEST 2019
property2=value2
property1=value1
property3=value3

The lines starting the # are comments. Notice the first line of the properties file is actually the comment that was passed as second parameter to the store() method call in the code example in the previous section about storing properties to a property file.

The lines following the key=value format contain the property key, value pairs.

Load Properties From File

You can also load properties stored in a property file back into a Java Properties object via its load() method. Here is an example of loading a property file into a Java Properties object:

Properties properties = new Properties();

try(FileReader fileReader = new FileReader("data/props.properties")){
    properties.load(fileReader);
} catch (IOException e) {
    e.printStackTrace();
}

Store Properties to XML File

The Java Properties class can also write the key-value pairs stored in it to an XML file via its storeToXML(). Here is an example of storing a Java Properties to an XML file:

Properties properties = new Properties();

properties.setProperty("property1", "value1");
properties.setProperty("property2", "value2");
properties.setProperty("property3", "value3");

try(FileOutputStream output = new FileOutputStream("data/props.xml")){
    properties.storeToXML(output, "These are properties");
} catch (IOException e) {
    e.printStackTrace();
}

XML Property File Format

The Java Properties object stored to XML file in the example in the previous section looks like this:

<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>These are properties</comment>
<entry key="property2">value2</entry>
<entry key="property1">value1</entry>
<entry key="property3">value3</entry>
</properties>

Notice how the comment passed to the storeToXML() method is enclosed in a comment XML element, and not in an XML comment (<!-- --> .

Load Properties from XML File

You can load properties from an XML property file into a Java Properties object via its loadFromXML() method. Here is an example of loading properties from an XML property file into a Java Properties object:

Properties properties = new Properties();
try(FileInputStream fileInputStream = new FileInputStream("data/props.xml")){
    properties.loadFromXML(fileInputStream);
} catch(IOException e){
    e.printStackTrace();
}

Jakob Jenkov




Copyright  Jenkov Aps
Close TOC