Properties class

The Properties class extends the Hashtable class and implements the Map interface.It represents the persistent set of properties.
Each key and its  value associated with it in the property list is a string.This class has the following constructors:-

public Properties()
public Properties(Properties def)
The first constructor constructs an empty property list with no default values.

The  second constructor will create a empty property with a default def. The property list def is searched if the property key is not found in the original property list.

Method Description
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.

Below example shows how to use Properties class to set key and value pairs, and get the value.

   import    java.util.*;
   public      class    Test {
   public      static      void    main(String[] args)  throws    Exception{
 Properties p=  new    Properties();

Below example shows how to use Properties class to get configuration values, from a properties file.

   import    java.util.*;
   public      class    LoadDemo {
   public      static      void    main(String[] args)  throws    Exception{
     FileReader reader=  new    FileReader(“”);

     Properties p=  new    Properties();


Below example shows how to use Properties class to set key and value pairs, to a properties file.

 import java.util.*;
 public class PropertiesExample {
           public static void main(String args[]) {
      try {
   Properties std_code = new Properties();

     grades.setProperty(“Bangalore”, “080”);
    grades.setProperty(“Delhi”, “011”);
     grades.setProperty(“Mumbai”, “022”);
    grades.setProperty(“Chennai”, “044”);
    grades.setProperty(“Kolkata”, “033”);

   // Save the grades properties using store() and an output stream
  FileOutputStream out = new FileOutputStream “”);, null);

  // load the properties file using load() and an input stream
   FileInputStream in = new FileInputStream(“stdcodes.propertie  std_code.load(in);

    // iterate properties file to get key-value pairs
     for (String key : std_code.stringPropertyNames()) {
     String value = std_code.getProperty(key);
      System.out.println(“The STD code of  ” + key + ” is: ” + value);

       } catch (IOException eio) {

Below example shows how to retrieve and store Configuration properties in a XML file.
NOTE: Below is sample XML file format

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "">
<comment>This is to test</comment>
<entry key="pname1">pval1</entry>
<entry key="pname2">pval</entry>
<entry key="pname3">pval3</entry>
<entry key="pname4">pval4</entry>
 import java.util.*;

 public class PropertiesloadfromXMLDemo {

    public static void main(String[] args) {
    Properties pro = new Properties();

    // add some properties
    pro.put("pname1", "pval1");
    pro.put("pname2", "pval2");

    try {

    // create a output and input as a xml file
    FileOutputStream fos = new FileOutputStream("abcproperties.xml");
    FileInputStream fis = new FileInputStream("abcproperties.xml");

    // store the properties in the specific xml
    pro.storeToXML(fos, null);

    // load key/values from the xml

    // Now print all the key/values to Standard output

    } catch (IOException ex) {