Properties class Example in Java

By | 2015-06-18T15:59:40+05:30 June 18th, 2015|java|


The Properties class is a subclass of Hashtable. Usually Properties are configuration values managed as key/value pairs. The key identifier is used to retrieve the value like variable name used to retrieve the variable’s value.

We can use the Properties class whenever we need to store key/value pairs in which both the keys and values are strings. Some of the important features of the Properties class include:

  • Use with other classes – Properties class is used by a few other Java classes. For example, System.getProperties() returns a Properties object with environmental values.

  • Default property – The Properties class enables you to specify a default property. If a key is not associated with any value, the default property is returned.

The constructors in the Properties class are:

  • Properties(): Creates an empty Properties object without any default values.

  • Properties(Properties propDefault):Creates a Properties object and uses the default values provided in propDefault.

Methods in Properties class

Some of the important methods in the Properties class are:

  • Object setProperty(String k, String v): Maps the value v with the key k and returns the previous value associated with k.

  • String getProperty(String k):Obtains the value associated with k and returns it.

  • void store(OutputStream so, String ds) throws IOException: Writes the Property list to the output stream that is linked to so after writing ds.

  • void load(InputStream si) throws IOException: Reads a Property list form the input stream that is linked to si.

Reading System Properties using Properties class

package com.onlinetutorials;

import java.util.Properties;

public class SystemPropertiesDemo {

    public static void main(String[] args) {
        Properties properties = System.getProperties();

On the above example displays the current System properties like below.

— listing properties —
java.library.path=/usr/lib/jvm/java-8-oracle/jre/lib/am… Platform API Specification
java.class.version=52.0 64-Bit Tiered Compilers
java.specification.vendor=Oracle Corporation
awt.toolkit=sun.awt.X11.XToolkit mode
java.vendor=Oracle Corporation

Getting properties from properties class

package com.onlinetutorials;

import java.util.Properties;

public class PropertiesReadDemo {

    public static void main(String[] args) throws Exception {
        FileReader file = new FileReader("");
        Properties properties = new Properties();
        System.out.println("userName : " + properties.getProperty("username"));
        System.out.println("password : " + properties.getProperty("password"));


userName : onlinetutorialspoint
password : 123456

On the above example, by using the FileReader object we are reading the file. After reading the properties file, we need to load the file into properties class in order  to get property values. By using the load() method we can load the property file into Java Properties.

 Writing data to properties class dynamically

We can also create a properties file dynamically like below.

package com.onlinetutorials;

import java.util.Properties;

public class PropertiesWriteDemo {

    public static void main(String[] args) throws Exception {
        Properties properties = new Properties();

        properties.setProperty("username", "online tutorial spoint");
        properties.setProperty("password", "123456"); FileWriter(""), "Db-Configurations");

#Thu Jun 18 21:12:51 IST 2015
username=online tutorial spoint

On the above example, by using the store(Writer writer,String comments) we are writing the property list (key / element pairs) in to file.

About the Author:

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Leave A Comment