Properties Class in Java

Properties Class

The Properties Class extends the Hashtable class and implements the Map interface. The Properties Class maintains the data in key/value format. Both key and value are String values.  It is used to store properties permanently in a file. Its extension should be “.properties”.

Example: Program to implement available methods in Properties class.


import java.util.*;  
import java.io.*;  
public class PropDemo
{
       public static void main(String args[])
       {
            //create the properties objetc
            Properties pro = new Properties();
            String Id;
            String name;

            //adding the elements
            pro.put("101", "ABC");
            pro.put("104", "DEF");
            pro.put("102", "XYZ");
            System.out.println(pro);
            System.out.println(pro.getProperty("101"));
            System.out.println(pro.getProperty("102", "XYZ"));
            System.out.println(pro.getProperty("106"));
       }
}


Output:
{104=DEF, 102=XYZ, 101=ABC}
ABC
XYZ
null