Configuring an Application Using the Properties Class

   

Configuring an Application Using the Properties Class

The Properties class is an extension of Hashtable that restricts its keys and value objects to being strings. The intended use is to support a set of properties that can be easily read from and written to a stream, such as a text file. The System class uses this functionality to store system properties, but you can also use it to create and maintain your own property sets. A common use of this class is in maintaining a file of properties used to configure an application when it is launched.

You can create a new empty Properties object with the no-argument constructor:

 public Properties() 

You can also create a Properties object with a set of default properties. If the Properties object cannot find a requested property in its table, it searches the default properties table. The default properties are maintained independently and are not changed if you modify a corresponding entry in the Properties instance that refers to it. This means that multiple Properties objects can safely share the same default Properties object. To create a Properties object with a set of defaults, just pass the default Properties object to the constructor:

 public Properties(Properties defaultProps) 

Setting Properties

Because Properties extends Hashtable, it is possible to insert entries using the put and putAll methods . However, this allows for the possibility of non-string keys or values being used. You should instead use the setProperty method, which restricts its arguments to strings:

 public Object setProperty(String key, String value) 

Querying Properties

The getProperty method returns the string corresponding to a property name , or null if the property is not set:

 public String getProperty(String key) 

If the key is not found and you have assigned a default Properties object, that set is also checked for a match before returning. You can also call getProperty and specify a default value to be returned if the specified property is not set:

 public String getProperty(String key, String defaultValue) 

This default value is only returned if the property cannot be found in the Properties object or its default properties, if any are assigned.

In addition to requesting the value of a specific property, you can also retrieve an Enumeration of all the property names in a Properties object, including the default properties, with the propertyNames method:

 public Enumeration propertyNames() 

Saving and Retrieving Properties

You can maintain a set of application properties in a text file using the store and load methods of Properties. You should consider this approach for initialization parameters and user preferences that can be pulled out of your code and placed in a file. Providing access to configuration information in such a manner is an important part of making your programs flexible.

You save a properties file using the store method (the previously supported save method was deprecated when Java 1.2 was released):

 public void store(OutputStream out, String header) throws IOException 

This method saves the properties to the specified stream, which is typically an instance of FileOutputStream. The header string is written to the stream before the contents of the Properties object to, for example, identify the purpose of the file.

You read a properties file using the corresponding load method:

 public void load(InputStream in) throws IOException 

The load method treats # and ! as comment characters and ignores anything after them on a line. You can use the \ character as a line continuation indicator to split a property value string across multiple lines in a file.

The following lines show an example set of properties created using the store method:

 #Sample Properties File #Wed Aug 16 13:59:42 EDT 2000 root=\projects\deploy\} colorScheme=red, green, blue, yellow password=1234 username=jsmith 

Although the colorScheme property in this example appears to have four values, the load method assigns a single string with the value "red, green, blue, yellow" to this property. This format allows you to associate a set of values with a property, but you must parse the string yourself.

The list methods of Properties are similar to store, but they output the properties in a more readable form. They produce a format that works well for debug use. These methods have the following signatures:

 public void list(PrintStream out) public void list(PrintWriter out) 
   


Special Edition Using Java 2 Standard Edition
Special Edition Using Java 2, Standard Edition (Special Edition Using...)
ISBN: 0789724685
EAN: 2147483647
Year: 1999
Pages: 353

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net