![]() ![]() ![]() ![]() |
Setting Program Attributes |
You can use the Properties class from java.util to manage attributes specific to your Java programs. A Properties object manages a set of key/value pairs: the key represents the name of a property and the value is the current value of the property. You can load key/value pairs into a Properties object from a stream, save the properties to a stream, and get information about the properties represented by the Properties object.Setting up Your Properties Object
Often when a program starts up, it will use code similar to the following to set up the properties object:First the application sets up a default Properties object. This object contains the set of properties to use if values are not explicitly set elsewhere. This code snippet uses the// set up default properties Properties defaultProps = new Properties(); FileInputStream defaultStream = new FileInputStream("defaultProperties"); defaultProps.load(defaultStream); defaultsStream.close(); // set up real properties Properties applicationProps = new Properties(defaultProps); FileInputStream appStream = new FileInputStream("appProperties"); applicationProps.load(appStream); appStream.close();load()
method to read the default values from a file on disk nameddefaultProperties
. Applications usually save and restore properties to files on the disk.Next, the program uses a different constructor to create a second Properties object,
applicationProps
. This object usesdefaultProps
to provide its default values.Then the code snippet loads a set of properties into
applicationProps
from a file namedappProperties
. The properties loaded intoappProperties
can be set on a per user basis, a per site basis, or whatever is appropriate for the current application. What's important is that the application saves the Properties to a "well-known" location so that the next invocation of the application can retrieve them. For example, the HotJava browser saves properties on a per user bases and saves the properties to a file in the user's home directory.You use the
save()
method to write properties to a stream:TheFileOutputStream defaultsOut = new FileOutputStream("defaultProperties"); applicationProps.save(defaultsOut, "---No Comment---"); defaultsOut.close();save()
method needs a stream to write to, and a string which it uses as a comment at the top of the output.Getting Property Information
Once you've set up your Properties object, you can query it for information about various properties it contains. The Properties class provides several methods for getting property information:
getProperty()
(2 versions)- returns the value for the specified property. One version allows you to provide a default value--if the key is not found the default is returned.
list()
- writes all the properties to the specified stream. This is useful for debugging.
propertyNames()
- returns an Enumeration containing all of the keys contained in the Properties object.
See also
java.util.Properties
![]() ![]() ![]() ![]() |
Setting Program Attributes |