Java Properties Class
Properties inherits from Hashtable. It represents a persistent set of properties. Each key and its corresponding value in the property list is a string.
The Properties class is used by many Java classes. For example, it is returned by the System.getProperties() method when getting environment variables.
Properties defines the following instance variable. This variable holds the default property list associated with a Properties object.
Properties defaults;
The Properties class defines two constructors. The first constructor has no default values.
Properties()
The second constructor uses propDefault as the default value. In both cases, the property list is empty:
Properties(Properties propDefault)
In addition to the methods defined by Hashtable, Properties defines the following methods:
Number | Method Description |
---|---|
1 | String getProperty(String key) <br> Searches for the property with the specified key in this property list. |
2 | String getProperty(String key, String defaultProperty) <br> Searches for the property with the specified key in this property list. |
3 | void list(PrintStream streamOut) <br> Prints the property list out to the specified output stream. |
4 | void list(PrintWriter streamOut) <br> Prints the property list out to the specified output stream. |
5 | void load(InputStream streamIn) throws IOException <br> Reads a property list (key and element pairs) from the input stream. |
6 | Enumeration propertyNames() <br> Reads a property list (key and element pairs) from the input character stream in a simple, line-oriented format. |
7 | Object setProperty(String key, String value) <br> Calls the Hashtable method put. |
8 | void store(OutputStream streamOut, String description) <br> Writes this property list (key and element pairs) to the output stream in a format suitable for loading into a Properties table using the load(InputStream) method. |
Example
The following program illustrates several 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
String str = capitals.getProperty("Florida", "Not Found");
System.out.println("The capital of Florida is "
+ str + ".");
}
}
The above example compiles and runs with the following output:
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.