Properties class in java with example

Points related to Properties class.

  • Properties class introduced in jdk1.0 extends Hashtable class.
  • Properties class is synchronized as all methods are synchronized.
  • This class store property in a simple XML format(UTF-8 character encoding is used) using storeToXML(OutputStream, String, String) method.

 

Example of Properties class –

Example 1 –

package propertiesclass;
import java.util.*;
public class Example1 {
public static void main(String[] args) {
	
	//creating Properties object 
	Properties propertiesObj = new Properties();
	propertiesObj.put("2", "ram");
	propertiesObj.put("4", "mohan");
	propertiesObj.put("6", "sohan");
	propertiesObj.put("5", "rohan");
	propertiesObj.put("1", "mohit");
	
	for (Object key : propertiesObj.keySet()) {
		System.out.println("Key is : " + key + "   Value is : " + propertiesObj.get(key));
	}
	
}
}

Output is –

Key is : 6 Value is : sohan
Key is : 5 Value is : rohan
Key is : 4 Value is : mohan
Key is : 2 Value is : ram
Key is : 1 Value is : mohit

 

Example 2 – getting Properties class reference using System.getProperties()

package propertiesclass;
import java.util.*;
public class Example2 {
	
public static void main(String[] args) {
	
	Properties p = System.getProperties();
	System.out.println(p);
}
}