|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
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 "Solaris".The
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. EachPropertieskey contains the name of a system attribute, and its correspondingPropertiesvalue is the current value of that attribute.The
Systemclass uses aPropertiesobject for managing system properties. Any Java program can use aPropertiesobject to manage its program attributes. ThePropertiesclass 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
Propertiesextends theHashtablePropertiesclass and inherits methods from it for doing the following:
- Testing to see if a particular key or value is in the
Propertiesobject- Getting the current number of key/value pairs
- Removing a key and its value
- Adding a key/value pair to the
Propertieslist- 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 Restrictionsfor information about security restrictions on applets.
PropertiesThe following figure illustrates how a typical program might manage its attributes with aPropertiesobject 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.
- Running
- 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.- Exiting
- Upon exiting, the program saves the properties to its well-known location, to be loaded again when the program is next started up.
Properties ObjectThe 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 nameddefaultProperties.Next, the application uses a different constructor to create a second
Propertiesobject,applicationProps, whose default values are contained indefaultProps. The defaults come into play when a property is being retrieved. If the property can't be found inapplicationProps, then its default list is searched.Finally, the code loads a set of properties into
applicationPropsfrom a file namedappProperties. 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).
PropertiesThe following example writes out the application properties from the previous example usingProperties'sstoremethod. 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.
Note: Thestoremethod was introduced to thePropertiesclass in JDK 1.2. If you are using an earlier release, use thesavemethod instead.
Once you've set up yourPropertiesobject, you can query it for information about various keys/values that it contains. An application gets information from aPropertiesobject after start up so that it can initialize itself based on choices made by the user. ThePropertiesclass has several methods for getting property information:
- contains(Object value)
containsKey(Object key)- Returns
trueif the value or the key is in thePropertiesobject.Propertiesinherits these methods fromHashtable. Thus they acceptObjectarguments. You should pass inStrings.- 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)
list(PrintWriter w)- Writes all of the properties to the specified stream or writer. This is useful for debugging.
- elements()
keys()
propertyNames()- Returns an
Enumerationcontaining the keys or values (as indicated by the method name) contained in thePropertiesobject.- size()
- Returns the current number of key/value pairs.
PropertiesA user's interaction with a program during its execution may impact property settings. These changes should be reflected in thePropertiesobject so that they are saved when the program exits (and calls thestoremethod). You can use the following methods to change the properties in aPropertiesobject:Both
- put(Object key, Object value)
- Puts the key/value pair in the
Propertiesobject.- remove(Object key)
- Removes the key/value pair associated with key.
putandremoveHashtableand thus takeObjects. You should pass inStrings.
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 |
Search
Feedback Form |
Copyright 1995-2004 Sun Microsystems, Inc. All rights reserved.