Using java.util.Properties class a developer can manage application configurations. The Properties class extends the java.util.HashTable class, which manages key/value pairs of both the Object class. The properties class maintain similar key/value pairs of Strings. The keys represent the ‘name of the property’ whereas the value represent the ‘value of the property’.

The Properties class provide methods for doing the following:

  • storing the properties list to an OutputStream (e.g. file) or an XML file.
  • retrieve the properties list from an InputStream (e.g. file) or an XML file.
  • retrieve a property value from the list if given a property name
  • print the list of all the properties to a PrintStream (e.g. Console)
  • return an enumeration of all the keys from the properties list.

The properties class can return a default value for a property if the requested property’s value cannot be found.

To initialize a properties class simply do the following:

Properties properties = new Properties();

To add key value pairs i.e. property name its value simply do the following:

properties.setProperty(“name”, “value”);

Default Properties:

To create default properties, simply instintiate a properties object as shown above.

Properties defaultProperties = new Properties();

Set its default values as shown above.


Now these default properties can be supplied to another Properties object as defaults as follows:

Properties prop = new Properties(defaultProperties);

If you call the prop.getProperty(“file-name”) method on the prop the properties.prop will be returned, since the prop object does not have that in its values so it will check the default properties. If found it will return the default property otherwise a null will be returned.

Example 1:

The following is a very simple example of handling properties with defaults in java:

* This class creates default properties & handles retrieving/updating properties

public class PropertyManager {

private Properties properties;

public PropertyManager() {

properties = new Properties(getDefaultProperties());


public String getProperty(String name) {

return properties.getProperty(name);


public void setProperty(String name, String value) {

properties.setProperty(name, value);


private Properties getDefaultProperties() {

Properties defaultProperties = new Properties();
defaultProperties.setProperty(“file-name”, “properties.prop”);
defaultProperties.setProperty(“version”, “1.3.3”);
defaultProperties.setProperty(“author”, “admin”);
return defaultProperties;



* This class uses the ProperyManager to retrieve / update its properties. If it requires a property it
* will supply the PropertyManager’s getProperty() method the property name. The PropertyManger
* will check its properties object; if it find one it will retrieve it, if not then it will check its default
* properties which will be returned otherwise a null will be returned.

public class PropertiesUser {

private static PropertyManager propManĀ  = new PropertyManager();

public static void main(String[] args) {

propMan.setProperty(“version”, “2.1”); //put the version property and set its value as “2.1”
System.out.println(“File Name:” + propMan.getProperty(“file-name”)); //print the filename, default will be used
System.out.println(“Version:” + propMan.getProperty(“version”)); //print the version,
System.out.println(“Author:” + propMan.getProperty(“author”)); //print the author, default will be used.



The output for this program will be as follows:


File Name:properties.prop


The PropertyUser class does not set the PropertyManager’s properties object with “file-name” property’s value so the default property value is returned. And set’s version” as “2.1” so “2.1” is returned.

Saving properties to a file:

Properties can be saved to a file by calling the Properties class store method. e.g.

PrintWriter writer = new PrintWriter(“prop.p”);,”Properties Example”);

Properties can be retrieved from a file by calling the Properties class load method e.g.

FileReader reader = new FileReader(“prop.p”);

You can then call the get and set and use these properties in you program as shown in the program above.

Tagged on:                     

5 thoughts on “Managing application configurations using the Java Properties Class

  • July 11, 2009 at 2:54 am

    Thank you for useful information.

  • August 8, 2009 at 1:53 am

    Valuable thoughts and advices. I read your topic with great interest.

  • September 30, 2009 at 8:06 pm

    Thats very good to know… thanks

  • November 2, 2009 at 1:24 am

    I really like your blog and i respect your work. I’ll be a frequent visitor.

  • November 13, 2009 at 12:38 am

    I read a few topics. I respect your work and added blog to favorites.
    buy clomid


Leave a Reply

Your email address will not be published. Required fields are marked *

Error. Page cannot be displayed. Please contact your service provider for more details. (30)