Properties Java


We know earlier Hashtable stores key/value pairs. There exists one more similar data structure that stores key/value pairs – Properties. Infact, Properties is derived from Hashtable. They differ very slightly. The key/value pair in Hasahtable can be objects of any Java class. But incase of Properties, the key/value pair should be strings only.

Following is the class signature

public class Properties extends Hashtable

As Properties is a subclass of Hashtable, it can utilize all the methods of Hashtable.

Two programs are given on Properties.

1. First program, PropMethods that use the methods of Properties.
2. Second program, PropSystem that reads system properties and prints.

The following is the first program using the Properties Java methods.

import java.util.*;
public class PropMethods 
{
  public static void main(String args[])
  {
    Properties prop = new Properties( );
    prop.put("ten", "10");
    prop.put("twenty", "20");
    prop.put("thirty", "30");

    System.out.println("Value of ten: " + prop.getProperty("ten"));  	            
    System.out.println("Value of forty: " + prop.getProperty("forty"));

    System.out.println("\nPrinting key/value pairs using Enumeration:");					
    Enumeration e = prop.keys( );
    while(e.hasMoreElements( ) )
    {
      String str1 = (String) e.nextElement( );
      System.out.println(str1 + " in words is " + prop.getProperty(str1));
    }
  }
}

Properties Java

Properties prop = new Properties( );
prop.put("ten", "10");

A Properties object prop is created and added three key/value pairs with put() method, the same method we used in Hashtable. But here, key/value pairs must be strings only.

System.out.println("Value of ten: " + prop.getProperty("ten"));
System.out.println("Value of forty: " + prop.getProperty("forty"));

getProperty("ten") method returns 10, the value associated with key "ten". getProperty("forty") returns null as no key by name "forty" exists with prop object (we have not added it, infact).

       Enumeration e = prop.keys( );
       while(e.hasMoreElements( ) )
       {
         String str1 = (String) e.nextElement( );
         System.out.println(str1 + " in words is " + prop.getProperty(str1));
       }

The keys() method of Properties returns an object of Enumeration. Using the methods hasMoreElements() and nextElement() all the keys and values are printed. The working methodology of Enumeration is discussed very clearly in "Enumeration Interface".

2. Second program, PropSystem that reads system properties and prints.

The Properties Java object can best used to read system properties. Following program explains.

import java.util.Properties;
import java.util.Enumeration;
public class PropSystem
{
  public static void main(String args[])
  {
    Properties prop = System.getProperties();
    Enumeration e = prop.keys( );
    while(e.hasMoreElements( ) )
    {
      String key = (String) e.nextElement( );
      String value = prop.getProperty(key);
      System.out.println(key + " : " + value);
    }
  }
}

Properties Java

(Screenshot showing a part of program’s output)

Properties prop = System.getProperties();

getProperties() is a static method of System class that returns a Properties object containing all the properties of underlying (here, Windows) operating system. From Properties object prop, using Enumeration, all the keys (system property name) and values (corresponding value of the property) are printed. As the properties are many, a few are given in the screenshot.

1 thought on “Properties Java”

Leave a Comment

Your email address will not be published.