Java The Complete Reference, Seventh Edition

(Greg DeLong) #1

498 Part II: The Java Library


The following example demonstratesProperties. It creates a property list in which the keys
are the names of states and the values are the names of their capitals. Notice that the attempt
to find the capital for Florida includes a default value.

// 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();

Method Description
String getProperty(Stringkey) Returns the value associated withkey.Anullobject is returned ifkeyis
neither in the list nor in the default property list.
String getProperty(Stringkey,
StringdefaultProperty)

Returns the value associated withkey. defaultPropertyis returned ifkeyis
neither in the list nor in the default property list.
void list(PrintStreamstreamOut) Sends the property list to the output stream linked tostreamOut.
void list(PrintWriterstreamOut) Sends the property list to the output stream linked tostreamOut.
void load(InputStreamstreamIn)
throws IOException

Inputs a property list from the input stream linked tostreamIn.

void load(ReaderstreamIn)
throws IOException

Inputs a property list from the input stream linked tostreamIn. (Added by
Java SE 6.)
void loadFromXML(InputStreamstreamIn)
throws IOException,
InvalidPropertiesFormatException

Inputs a property list from an XML document linked tostreamIn.

Enumeration<?> propertyNames( ) Returns an enumeration of the keys. This includes those keys found in
the default property list, too.
Object setProperty(Stringkey, Stringvalue) Associatesvaluewithkey.Returns the previousvalue associated withkey,
or returns null if no such association exists.
void store(OutputStreamstreamOut,
Stringdescription)
throws IOException

After writing the string specified bydescription,the property list is written
to the output stream linked tostreamOut.

void store(WriterstreamOut,
Stringdescription)
throws IOException

After writing the string specified bydescription, the property list is written
to the output stream linked tostreamOut. (Added by Java SE 6.)

void storeToXML(OutputStreamstreamOut,
Stringdescription)
throws IOException

After writing the string specified bydescription,the property list is written
to the XML document linked tostreamOut.

void storeToXML(OutputStreamstreamOut,
Stringdescription,
Stringenc)

The property list and the string specified bydescriptionis written to the
XML document linked tostreamOutusing the specified character
encoding.
Set<String> stringPropertyNames( ) Returns a set of keys. (Added by Java SE 6.)

TABLE 17-19 The Methods Defined byProperties
Free download pdf