Properties
The
java.util.Properties
class is a
specialized
hashtable
for strings. Java uses the
Properties object to replace the environment
variables used in other programming environments. You can use a
Properties object (or “table”) to hold
arbitrary configuration information for an application in an easily
accessible format. The Properties object can also
load and store information using streams (see Chapter 10, for information on streams).
Any string values can be stored as key/value pairs in a
Properties table. However, the convention is to
use a
dot-separated naming hierarchy to
group property names into logical structures, as is done with X
Window System resources on Unix systems.[36] The java.lang.System
class provides system-environment information in this way, through a
system Properties table we’ll describe
shortly.
Create an empty Properties table and add
String key/value pairs just as with any
Hashtable:
Properties props = new Properties( );
props.put("myApp.xsize", "52");
props.put("myApp.ysize", "79");
Thereafter,
you can retrieve values with the getProperty( )
method:
String xsize = props.getProperty( "myApp.xsize" );
If the named property doesn’t exist,
getProperty( ) returns null.
You can get an Enumeration of the property names
with the propertyNames( ) method:
for ( Enumeration e = props.propertyNames( ); e.hasMoreElements; ) {
String name = e.nextElement( );
...
}Default Values
When you create a
Properties table, you can specify a second table for default property ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access