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. Although the Properties class, itself, is not generic, several of its methods are.
Properties defines the following instance variable:
Properties defaults;
This variable holds a default property list associated with a Properties object. Properties defines these constructors:
Properties( )
Properties(Properties propDefault)
Table of The Methods Defined by Properties
Method | Description | ||
String getProperty(String key) | Returns the value associated with key. A null object is returned if key is | ||
neither in the list nor in the default property list. | |||
String getProperty(String key, | Returns the value associated with key. defaultProperty is returned if key is | ||
String defaultProperty) | 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) | Inputs a property list from the input stream linked to streamIn. | ||
throws IOException | |||
void load(Reader streamIn) | Inputs a property list from the input stream linked to streamIn. (Added by | ||
throws IOException | Java SE 6.) | ||
void loadFromXML(InputStream streamIn) | Inputs a property list from an XML document linked to streamIn. | ||
throws IOException, | |||
InvalidPropertiesFormatException | |||
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 key. Returns the previous value associated with key, | ||
or returns null if no such association exists. | |||
void store(OutputStream streamOut, | After writing the string specified by description, the property list is written | ||
String description) | to the output stream linked to streamOut. | ||
throws IOException | |||
void store(Writer streamOut, | After writing the string specified by description, the property list is written | ||
String description) | to the output stream linked to streamOut. (Added by Java SE 6.) | ||
throws IOException | |||
void storeToXML(OutputStream streamOut, | After writing the string specified by description, the property list is written | ||
String description) | to the XML document linked to streamOut. | ||
throws IOException | |||
void storeToXML(OutputStream streamOut, | The property list and the string specified by description is written to the | ||
String description, | XML document linked to streamOut using the specified character | ||
String enc) | encoding. | ||
Set<String> stringPropertyNames( ) | Returns a set of keys. (Added by Java SE 6.) |
Demonstrate a Property list
import java.util.*;
class PropDemo {
public static void main(String args[]) { Properties capitals = new Properties();
capitals.put("Illinois", "Springfield"); capitals.put("Missouri", "Jefferson City"); capitals.put("Washington", "Olympia"); capitals.put("California", "Sacramento"); capitals.put("Indiana", "Indianapolis");
//Get a set-view of the keys. Set states = capitals.keySet();
//Show all of the states and capitals. for(Object name : states)
System.out.println("The capital of " +
name + " is " + capitals.getProperty((String)name) + ".");
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 output from this program is shown here:
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.
Use a default property list
import java.util.*;
class PropDemoDef {
public static void main(String args[]) { Properties defList = new Properties(); defList.put("Florida", "Tallahassee"); defList.put("Wisconsin", "Madison");
Properties capitals = new Properties(defList);
capitals.put("Illinois", "Springfield"); capitals.put("Missouri", "Jefferson City"); capitals.put("Washington", "Olympia"); capitals.put("California", "Sacramento"); capitals.put("Indiana", "Indianapolis");
//Get a set-view of the keys. Set states = capitals.keySet();
//Show all of the states and capitals. for(Object name : states)
System.out.println("The capital of " +
name + " is " + capitals.getProperty((String)name) + ".");
System.out.println();
//Florida will now be found in the default list. String str = capitals.getProperty("Florida"); System.out.println("The capital of Florida is "
+str + ".");
}
}
Using store( ) and load( )
A simple telephone number database that uses a property list
import java.io.*; import java.util.*;
class Phonebook {
public static void main(String args[]) throws IOException
{
Properties ht = new Properties(); BufferedReader br =
new BufferedReader(new InputStreamReader(System.in)); String name, number;
FileInputStream fin = null; boolean changed = false;
//Try to open phonebook.dat file. try {
fin = new FileInputStream("phonebook.dat"); } catch(FileNotFoundException e) {
// ignore missing file
}
/* If phonebook file already exists, load existing telephone numbers. */
try {
if(fin != null) {
ht.load(fin);
fin.close();
}
} catch(IOException e) { System.out.println("Error reading file.");
}
//Let user enter new names and numbers. do {
System.out.println("Enter new name" +
"('quit' to stop): ");
name = br.readLine(); if(name.equals("quit")) continue;
System.out.println("Enter number: "); number = br.readLine();
ht.put(name, number); changed = true;
} while(!name.equals("quit"));
//If phone book data has changed, save it. if(changed) {
FileOutputStream fout = new FileOutputStream("phonebook.dat");
ht.store(fout, "Telephone Book"); fout.close();
}
//Look up numbers given a name. do {
System.out.println("Enter name to find" +
"('quit' to quit): ");
name = br.readLine(); if(name.equals("quit")) continue;
number = (String) ht.get(name); System.out.println(number);
} while(!name.equals("quit"));
}
}