Properties

Java provides a feature through which a program can store and load the specific configurable data. Program can change the behavior based on the property. You can store textual information to Properties and you can use latter.

Java has a class Properties that store the data in the form of hash table. Properties object is used to store or retrieve the information in the form of name-value pairs. Any string values can be stored as key/value pairs in a Properties table. However, the convention is to use a dot-separated naming hierarchy to group property names into logical structures.

Working with properties

First you have to create an object of the properties class. for example.

            Properties prop=new Properties();
After that you can store the values by using the setter methods
     prop.setProperty(“my.name”,”ABC xyz”);
     prop.setProperty(“my.phone”,”878897897”);

Here my.name is the property name or key and ABC xyz is the value to be stored. Similarly my.phone is the property name or key and 878897897 is the value.

Thereafter, you can retrieve values with the getProperty( ) method:
    String name = props.getProperty( "my.name" );

If the named property doesn't exist, getProperty( ) returns null. You can get an Enumeration of the property names with the propertyNames( ) method:
    for ( Enumeration e = props.propertyNames(  ); e.hasMoreElements; ) {
        String name = e.nextElement(  );
        ...
    }


Loading and storing Properties


You can load properties from the any specified InputStream. The Properties are loaded at the time of instantiation of Properties Object. To load the properties we use the load() method as shown below

prop.load(InputStream in);

The properties holding by properties object at the run time can be stored into the external files. You can send properties to any output stream.

prop.store(OutputStream out);

Storing Properties

import java.util.*;
import java.io.*;

class PropertiesDemo
{
    public static void main(String ar[])
    {
        Properties prop=new Properties();
        try{
            prop.setProperty("my.name","ABC xyz");
            prop.setProperty("my.phone","3453545");
           
            FileOutputStream fout=new  FileOutputStream("Prop.properties");
            prop.store(fout,"My Properties");
           
        }catch(Exception e){}
    }
}


Loading properties

import java.util.*;
import java.io.*;

class PropertiesDemo2
{
    public static void main(String ar[])
    {
        Properties prop=new Properties();
        try{
            FileInputStream fin=new  FileInputStream("Prop.properties");
            prop.load(fin);
           
            String name=prop.getProperty("my.name");
            String phone=prop.getProperty("my.phone");
           
            System.out.println("name="+name);
            System.out.println("phone="+phone);
           
        }catch(Exception e){}
    }
}
/
name=ABC xyz
phone=3453545
*/
Java 1.5 has new feature to write the properties into XML files. See the following programs:

import java.util.*;
import java.io.*;

class PropertiesDemo3
{
    public static void main(String ar[])
    {
        Properties prop=new Properties();
        try{
            prop.setProperty("my.name","ABC xyz");
            prop.setProperty("my.phone","3453545");
           
            FileOutputStream fout=new  FileOutputStream("Prop.xml");
            prop.storeToXML(fout,"My Properties");
           
        }catch(Exception e){}
    }
}
following contents are written
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>My Properties</comment>
<entry key="my.phone">3453545</entry>
<entry key="my.name">ABC xyz</entry>
</properties>
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
import java.util.*;
import java.io.*;

class PropertiesDemo4
{
    public static void main(String ar[])
    {
        Properties prop=new Properties();
        try{
            FileInputStream fin=new  FileInputStream("Prop.xml");
            prop.loadFromXML(fin);
            String name=prop.getProperty("my.name");
            String phone=prop.getProperty("my.phone");
            System.out.println("name="+name);
            System.out.println("phone="+phone);
        }catch(Exception e){}
    }
}
/
name=ABC xyz
phone=3453545
*/

No comments:

Popular Posts