|Start of Tutorial > Start of Trail > Start of Lesson||
An attribute has two parts: a name and a value. For example, "
os.name" is the name for one of the Java platform's system attributes; its value contains the name of the current operating system, such as "
Propertiesclass in the
java.utilpackage manages a set of key/value pairs. A key/value pair is like a dictionary entry: The key is the word, and the value is the definition. This is a perfect match for managing the names and values of attributes. Each
Propertieskey contains the name of a system attribute, and its corresponding
Propertiesvalue is the current value of that attribute.
Systemclass uses a
Propertiesobject for managing system properties. Any Java program can use a
Propertiesobject to manage its program attributes. The
Propertiesclass itself provides methods for the following:
- Loading key/value pairs into a
Propertiesobject from a stream
- Retrieving a value from its key
- Listing the keys and their values
- Enumerating over the keys
- Saving the properties to a stream
HashtablePropertiesclass and inherits methods from it for doing the following:
- Testing to see if a particular key or value is in the
- Getting the current number of key/value pairs
- Removing a key and its value
- Adding a key/value pair to the
- Enumerating over the values or the keys
- Retrieving a value by its key
- Finding out if the
Propertiesobject is empty
Security Considerations: Access to properties is subject to approval by the current security manager. The example code segments in this section are assumed to be in standalone applications, which, by default, have no security manager. If you attempt to use this code in an applet, it may not work, depending on the browser or viewer in which it is running. See Security Restrictions for information about security restrictions on applets.
The following figure illustrates how a typical program might manage its attributes with a
Propertiesobject over the course of its execution.
- Starting Up
- The actions given in the first three boxes occur when the program is starting up. First, the program loads the default properties from a well-known location into a
Propertiesobject. Normally, the default properties are stored in a file on disk along with the
.classand other resource files for the program.
Next, the program creates another
Propertiesobject and loads the properties that were saved from the last time the program was run. Many applications store properties on a per-user basis, so the properties loaded in this step are usually in a specific file in a particular directory maintained by this application in the user's home directory. Finally, the program uses the default and remembered properties to initialize itself.
The key here is consistency. The application must always load and save properties to the same location so that it can find them the next time it's executed.
- During the execution of the program, the user may change some settings, perhaps in a Preferences window, and the
Propertiesobject is updated to reflect these changes. For them to have a permanent effect, they must be saved.
- Upon exiting, the program saves the properties to its well-known location, to be loaded again when the program is next started up.
The following Java code performs the first two steps described in the previous section: loading the default properties and loading the remembered properties:First, the application sets up a default. . . // create and load default properties Properties defaultProps = new Properties(); FileInputStream in = new FileInputStream("defaultProperties"); defaultProps.load(in); in.close(); // create program properties with default Properties applicationProps = new Properties(defaultProps); // now load properties from last invocation in = new FileInputStream("appProperties"); applicationProps.load(in); in.close(); . . .
Propertiesobject. This object contains the set of properties to use if values are not explicitly set elsewhere. Then the load method reads the default values from a file on disk named
Next, the application uses a different constructor to create a second
applicationProps, whose default values are contained in
defaultProps. The defaults come into play when a property is being retrieved. If the property can't be found in
applicationProps, then its default list is searched.
Finally, the code loads a set of properties into
applicationPropsfrom a file named
appProperties. The properties in this file are those that were saved from the program the last time it was invoked (the next section shows you how this was done).
The following example writes out the application properties from the previous example using
storemethod. The default properties don't need to be saved each time because they never change.TheFileOutputStream out = new FileOutputStream("appProperties"); applicationProps.store(out, "---No Comment---"); out.close();
storemethod needs a stream to write to, as well as a string that it uses as a comment at the top of the output.
storemethod was introduced to the
Propertiesclass in JDK 1.2. If you are using an earlier release, use the
Once you've set up your
Propertiesobject, you can query it for information about various keys/values that it contains. An application gets information from a
Propertiesobject after start up so that it can initialize itself based on choices made by the user. The
Propertiesclass has several methods for getting property information:
- contains(Object value)
trueif the value or the key is in the
Propertiesinherits these methods from
Hashtable. Thus they accept
Objectarguments. You should pass in
- getProperty(String key)
getProperty(String key, String default)
- Returns the value for the specified property. The second version allows you to provide a default value. If the key is not found, the default is returned.
- list(PrintStream s)
- Writes all of the properties to the specified stream or writer. This is useful for debugging.
- Returns an
Enumerationcontaining the keys or values (as indicated by the method name) contained in the
- Returns the current number of key/value pairs.
A user's interaction with a program during its execution may impact property settings. These changes should be reflected in the
Propertiesobject so that they are saved when the program exits (and calls the
storemethod). You can use the following methods to change the properties in a
- put(Object key, Object value)
- Puts the key/value pair in the
- remove(Object key)
- Removes the key/value pair associated with key.
removeHashtableand thus take
Objects. You should pass in
BINGO!, shows and documents a complete client/server application that implements the game of BINGO. Both the client and the server application in that example use
Propertiesto maintain program attributes.
|Start of Tutorial > Start of Trail > Start of Lesson||
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.