Java Properties Class Example

Oct 5, 2015
264 Views
Image 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.
Properties class in java, Properties class Example in java, System properties in java,Java Properties Class Example,Properties Class Example,Class Example,

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.
 Do you know Clone Object in Java

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.blogspot.geekonjava;
 
import java.util.Properties;
 
public class SystemPropertiesDemo {
 
    public static void main(String[] args) {
        Properties properties = System.getProperties();
        properties.list(System.out);
    }
}
For full article : http://geekonjava.blogspot.com/2015/10/java-properties-class-example.html
Comments
avatar
Please sign in to add comment.