Java in a Nutshell, 5th Edition [Electronic resources]

نسخه متنی -صفحه : 1191/ 742
نمايش فراداده

Propertiesjava.util

Java 1.0cloneable serializable collection

This class is an extension of Hashtable that allows key/value pairs to be read from and written to a stream. The Properties class implements the system properties list, which supports user customization by allowing programs to look up the values of named resources. Because the load( ) and store( ) methods provide an easy way to read and write properties from and to a text stream, this class provides a convenient way to implement an application configuration file.

When you create a Properties object, you may specify another Properties object that contains default values. Keys (property names) and values are associated in a Properties object with the Hashtable method put( ). Values are looked up with getProperty( ); if this method does not find the key in the current Properties object, it looks in the default Properties object that was passed to the constructor method. A default value can also be specified, in case the key is not found at all. Use setProperty( ) to add a property name/value pair to the Properties object. This Java 1.2 method is preferred over the inherited put( ) method because it enforces the constraint that property names and values be strings.

propertyNames( ) returns an enumeration of all property names (keys) stored in the Properties object and (recursively) all property names stored in the default Properties object associated with it. list( ) prints the properties stored in a Properties object, which can be useful for debugging. store( ) writes a Properties object to a stream, writing one property per line, in name=value format. As of Java 1.2, store( ) is preferred over the deprecated save( ) method, which writes properties in the same way but suppresses any I/O exceptions that may be thrown in the process. The second argument to both store( ) and save( ) is a comment that is written out at the beginning of the property file. Finally, load( ) reads key/value pairs from a stream and stores them in a Properties object. It is suitable for reading both properties written with store( ) and hand-edited properties files. In Java 5.0, storeToXML( ) and loadFromXML( ) are alternatives that write and read properties files using a simple XML grammar.

Figure 16-48. java.util.Properties

public class

Properties extends Hashtable<Object,Object> { // Public Constructors public

Properties ( ); public

Properties (Properties

defaults ); // Public Instance Methods public String

getProperty (String

key ); public String

getProperty (String

key , String

defaultValue );

1.1 public void

list (java.io.PrintWriter

out ); public void

list (java.io.PrintStream

out ); public void

load (java.io.InputStream

inStream ) throws java.io.IOException; synchronized

5.0 public void

loadFromXML (java.io.InputStream

in ) throws java.io.IOException, InvalidPropertiesFormatException; synchronized public Enumeration<?>

propertyNames ( );

1.2 public Object

setProperty (String

key , String

value ); synchronized

1.2 public void

store (java.io.OutputStream

out , String

comments ) throws java.io.IOException; synchronized

5.0 public void

storeToXML (java.io.OutputStream

os , String

comment ) throws java.io.IOException; synchronized

5.0 public void

storeToXML (java.io.OutputStream

os , String

comment , String

encoding ) throws java.io.IOException; synchronized // Protected Instance Fields protected Properties

defaults ; // Deprecated Public Methods

# public void

save (java.io.OutputStream

out , String

comments ); synchronized }

Subclasses

java.security.Provider

Passed To

System.setProperties( ), javax.xml.transform.Transformer.setOutputProperties( )

Returned By

System.getProperties( ), javax.xml.transform.Templates.getOutputProperties( ), javax.xml.transform.Transformer.getOutputProperties( )