Properties class Example in Java

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

Properties

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();
        properties.list(System.out);
    }
}

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

— listing properties —
os.name=Linux
sun.jnu.encoding=UTF-8
java.library.path=/usr/lib/jvm/java-8-oracle/jre/lib/am…
java.specification.name=Java Platform API Specification
java.class.version=52.0
sun.management.compiler=HotSpot 64-Bit Tiered Compilers
os.version=3.13.0-55-generic
user.home=/home/chandrashekhar
user.timezone=
java.awt.printerjob=sun.print.PSPrinterJob
file.encoding=UTF-8
java.specification.version=1.8
user.name=chandrashekhar
java.class.path=/home/chandrashekhar/…
java.vm.specification.version=1.8
sun.arch.data.model=64
java.home=/usr/lib/jvm/java-8-oracle/jre
sun.java.command=com.onlinetutorials.SystemPropertiesDemo
java.specification.vendor=Oracle Corporation
user.language=en
awt.toolkit=sun.awt.X11.XToolkit
java.vm.info=mixed mode
java.version=1.8.0_45
java.ext.dirs=/usr/lib/jvm/java-8-oracle/jre/lib/ex…
sun.boot.class.path=/usr/lib/jvm/java-8-oracle/jre/lib/re…
java.vendor=Oracle Corporation
file.separator=/
java.vendor.url.bug=http://bugreport.sun.com/bugreport/
sun.cpu.endian=little
sun.io.unicode.encoding=UnicodeLittle
sun.desktop=gnome
sun.cpu.isalist=

Getting properties from properties class

package com.onlinetutorials;

import java.io.FileReader;
import java.util.Properties;

public class PropertiesReadDemo {

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

Output:

userName : onlinetutorialspoint
password : 123456

On the above example, by using the FileReader object we are reading the dbprops.property 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");

        properties.store(new FileWriter("dbprops.properties"), "Db-Configurations");
    }
}

Output:
#Db-Configurations
#Thu Jun 18 21:12:51 IST 2015
password=123456
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 dbprops.properties 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