Latest web development tutorials

Java Properties Interface

Properties inherits from Hashtable. Represents a persistent set of properties. Each key and its corresponding value in the property list is a string.

Properties class used by many Java classes. For example, when it returns the value of the environment variables as System.getProperties () method.

Properties defined as an instance variable. This variable holds a list of default Properties object associated attributes.

Properties defaults;

Properties class defines two constructors. The first constructor no default.

Properties()

The second constructor uses propDefault as the default. In both cases, the attribute list is empty:

Properties(Properties propDefault)

Apart from the Hashtable method as defined in, Properties defines the following methods:

No. Method Description
1 String getProperty (String key)
Search properties in this property list with the specified key.
2 String getProperty (String key, String defaultProperty )
Search properties in the property list with the specified key.
3 void list (PrintStream streamOut)
Property list out to the specified output stream.
4 void list (PrintWriter streamOut)
Property list out to the specified output stream.
5 void load (InputStream streamIn) throws IOException
Reads a property list (key and element pairs) from the input stream.
6 Enumeration propertyNames ()
In a simple line-oriented format Reads a property list (key and element pairs) from the input character stream.
7 Object setProperty (String key, String value )
The method of the Hashtable call to put.
8 void store (OutputStream streamOut, String description )
Suitable for using the load (InputStream) method for loading into a Properties table format, this Properties table in the property list (key and element pairs) to the output stream.

Examples

The following program illustrates this data structure supports several methods:

import java.util.*;

public class PropDemo {

   public static void main(String args[]) {
      Properties capitals = new Properties();
      Set states;
      String str;
      
      capitals.put("Illinois", "Springfield");
      capitals.put("Missouri", "Jefferson City");
      capitals.put("Washington", "Olympia");
      capitals.put("California", "Sacramento");
      capitals.put("Indiana", "Indianapolis");

      // Show all states and capitals in hashtable.
      states = capitals.keySet(); // get set-view of keys
      Iterator itr = states.iterator();
      while(itr.hasNext()) {
         str = (String) itr.next();
         System.out.println("The capital of " +
            str + " is " + capitals.getProperty(str) + ".");
      }
      System.out.println();

      // look for state not in list -- specify default
      str = capitals.getProperty("Florida", "Not Found");
      System.out.println("The capital of Florida is "
          + str + ".");
   }
}

The above examples compiled results are as follows:

The capital of Missouri is Jefferson City.
The capital of Illinois is Springfield.
The capital of Indiana is Indianapolis.
The capital of California is Sacramento.
The capital of Washington is Olympia.

The capital of Florida is Not Found.