java tutorial - Properties class in Java - java programming - learn java - java basics - java for beginners



The properties object covers key and value pair as a string. The Properties class is the subclass of Hashtable.

  • It can be used to get property value based on the property key. It offers methods to get data from properties file and then store file. Also, it can be used to get properties of system.

Advantage of properties file

  • Recompilation is not compulsory, if information is changed from properties file: If any information is changed from the properties file, you don't need to recompile the java class.
  • It is used to store information which is to be changed regularly.

Methods of Properties class

The commonly used methods of Properties class are given below.

MethodDescription
public void load(Reader r)loads data from the Reader object.
public void load(InputStream is)loads data from the InputStream object
public String getProperty(String key)returns value based on the key.
public void setProperty(String key,String value)sets the property in the properties object.
public void store(Writer w, String comment)writers the properties in the writer object.
public void store(OutputStream os, String comment)writes the properties in the OutputStream object.
storeToXML(OutputStream os, String comment) writers the properties in the writer object for generating xml document.
public void storeToXML(Writer w, String
comment, String encoding)
writers the properties in the writer object for generating xml document with specified encoding.

Example of Properties class to get information from properties file

  • To get information from the properties file, create the properties file first.

db.properties

user=system  
password=oracle  
click below button to copy the code. By - java tutorial - team

Now, lets create the java class to read the data from the properties file.

Test.java

import java.util.*;  
import java.io.*;  
public class Test {  
public static void main(String[] args)throws Exception{  
    FileReader reader=new FileReader("db.properties");  
      
    Properties p=new Properties();  
    p.load(reader);  
      
    System.out.println(p.getProperty("user"));  
    System.out.println(p.getProperty("password"));  
}  
}  
click below button to copy the code. By - java tutorial - team
system
 oracle

Now if you change the value of the properties file, you don't need to compile the java class again. That means no maintenance problem.

Example of Properties class to get all the system properties

By System.getProperties() method we can get all the properties of system. Let's create the class that gets information from the system properties.

Test.java

import java.util.*;  
import java.io.*;  
public class Test {  
public static void main(String[] args)throws Exception{  
  
Properties p=System.getProperties();  
Set set=p.entrySet();  
  
Iterator itr=set.iterator();  
while(itr.hasNext()){  
Map.Entry entry=(Map.Entry)itr.next();  
System.out.println(entry.getKey()+" = "+entry.getValue());  
}  
  
}  
}  
click below button to copy the code. By - java tutorial - team

Output:

java.runtime.name = Java(TM) SE Runtime Environment
sun.boot.library.path = C:\Program Files\Java\jdk1.7.0_01\jre\bin
java.vm.version = 21.1-b02
java.vm.vendor = Oracle Corporation
java.vendor.url = http://java.oracle.com/
path.separator = ;
java.vm.name = Java HotSpot(TM) Client VM
file.encoding.pkg = sun.io
user.country = US
user.script = 
sun.java.launcher = SUN_STANDARD
...........

Example of Properties class to create properties file

Now lets write the code to create the properties file.

Test.java

import java.util.*;  
import java.io.*;  
public class Test {  
public static void main(String[] args)throws Exception{  
  
Properties p=new Properties();  
p.setProperty("name","Sonoo Jaiswal");  
p.setProperty("email","[email protected]");  
  
p.store(new FileWriter("info.properties"),"wikitechy Properties Example");  
  
}  
}  
click below button to copy the code. By - java tutorial - team
  • Let's see the generated properties file.

info.properties

#wikitechy Properties Example  
#Thu Oct 03 22:35:53 IST 2013  
[email protected]  
name=Sonoo Jaiswal  
click below button to copy the code. By - java tutorial - team

Related Searches to Properties class in Java