Java - The Properties Class

Properties is a subclass of Hashtable. It is used to maintain lists of values in which the key is a String and the value is also a String.
The Properties class is used by many other Java classes. For example, it is the type of object returned by System.getProperties( ) when obtaining environmental values.
Properties define the following instance variable. This variable holds a default property list associated with a Properties object.
Properties defaults;

Following is the list of constructors provided by the properties class.
Properties( )
This constructor creates a Properties object that has no default values.
Properties(Properties propDefault)
Creates an object that uses propDefault for its default values. In both cases, the property list is empty.
Apart from the methods defined by Hashtable, Properties define the following methods −
String getProperty(String key)
Returns the value associated with the key. A null object is returned if the key is neither in the list nor in the default property list.
String getProperty(String key, String defaultProperty)
Returns the value associated with the key; defaultProperty is returned if the key is neither in the list nor in the default property list.
void list(PrintStream streamOut)
Sends the property list to the output stream linked to streamOut.
void list(PrintWriter streamOut)
Sends the property list to the output stream linked to streamOut.
void load(InputStream streamIn) throws IOException
Inputs a property list from the input stream linked to streamIn.
Enumeration propertyNames( )
Returns an enumeration of the keys. This includes those keys found in the default property list, too.
Object setProperty(String key, String value)
Associates value with the key. Returns the previous value associated with the key, or returns null if no such association exists.
void store(OutputStream streamOut, String description)
After writing the string specified by description, the property list is written to the output stream linked to streamOut.
The following program illustrates several of the methods supported by this data structure −
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 + ".");
   }
}
This will produce the following result −
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.


    16 Lectures 
     2 hours 

 Malhar Lathkar

    19 Lectures 
     5 hours 

 Malhar Lathkar

    25 Lectures 
     2.5 hours 

 Anadi Sharma

    126 Lectures 
     7 hours 

  Tushar Kale

    119 Lectures 
     17.5 hours 

 Monica Mittal

    76 Lectures 
     7 hours 

 Arnab Chakraborty
 Print
 Add Notes
 Bookmark this page