Making use of the Properties class

In this section, let us make use of the Properties class for storing and loading application related configuration values as well as other user information. It is also possible to query or modify the various System properties using this class. For example, the following code tries to get all the properties that are available for the currently executing JVM.

also read:

Properties sysProperties = System.getProperties();

The return type of System.getProperties() is a Properties object that is loaded with the various system configuration values. For example, to dump all the contents of the system properties onto the console, use the following piece of code,

Properties sysProperties = System.getProperties();
sysProperties.list(System.out);

You can see a whole bunch of properties being shown at the console. A property in general is represented as a name-value pair. So, if you want to get a particular system property, then you can the getProperty() method by passing the key name. For example, the following code snippet tries to get the value of the currently running version of the java.

Properties sysProperties = System.getProperties();
String javaVersion = sysProperties.getProperty("java.version");
System.out.println("Java version is " + javaVersion);

Properties object support the notion of default values. For example, consider the following code snippet which will illustrate this,

Properties sysProperties = System.getProperties();
String appName =
sysProperties.getProperty("application.name", "Default App Name");
System.out.println("Application name is " + appName);

We are trying to get the value of the property application.name. But, what happens when there is no such property called application.name? This is where the second argument comes into picture. It is called the default property value. Where is a no such property, then the default property value is returned to the calling client.

Till now, we have seen how to deal with system related property names and values. But creating properties that are very specific to an application is as simple as the following example will illustrate that,

Properties appProperties = new Properties();
appProperties.setProperty('Application.name', 'Portal Application, ABC');
appProperties.setProperty('Application.client', 'Portal client');

We have explicitly created a Properties object and then have added two application specific properties into the appProperties object. This is one way of populating properties, the other way is loading property information from a file. Suppose we have all the property information in a file in the following format,

myprops.props

Application.name=Portal Application, ABC
Application.client=Portal client

Then the following code can be used to load the property information from the file,

try{
    Properties appProperties = new Properties();
    appProperties.load(new FileReader('./src/tips/properties/myprops.props'));
    appProperties.list(System.out);
}catch(Exception exception){
    exception.printStackTrace();
}

Right from Java 6.0, it is also possible to persistent the property values in Xml format. For example, consider the following code snippet that just does that,

try{
    Properties appProperties = new Properties();
    appProperties.load(new FileReader('./src/tips/properties/myprops.props'));
    appProperties.storeToXML(
        new FileOutputStream('./src/tips/properties/myprops1.props'),
            'Application Properties');
}catch(Exception exception){
exception.printStackTrace();
}

The storeToXML() method takes the file name where the properties has to be stored along with optional comment information. After running the program, a file called myprops1.props would have got created and following are its contents.

myprops1.props

<?xml version='1.0' encoding='UTF-8' standalone='no'?>
<!DOCTYPE properties SYSTEM 'http://java.sun.com/dtd/properties.dtd'>
<properties>
    <comment>Application Properties</comment>
    <entry key='Application.name'>Portal Application, ABC</entry>
    <entry key='Application.client'>Portal client</entry>
</properties>

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Comments

  1. Vikash Singh says:

    I am creating a login page and i want to pass variables info from one frame to another…………
    and i am getting trouble…..

    Can you please help me to short it…

Speak Your Mind

*