Properties Class
A Properties object is a persistent Hashtable that normally stores key-value pairs of stringsassuming that you use methods setProperty and getProperty to manipulate the table rather than inherited Hashtable methods put and get. By "persistent," we mean that the Properties object can be written to an output stream (possibly a file) and read back in through an input stream. In fact, most objects in Java can be output and input with Java's object serialization, presented in Chapter 14. A common use of Properties objects in prior versions of Java was to maintain application-configuration data or user preferences for applications. [Note: The Preferences API (package java.util.prefs), introduced in Java 1.4, is meant to replace the use of class Properties, but is beyond the scope of this book. To learn more, visit java.sun.com/j2se/5.0/docs/guide/lang/preferences.html.]
Class Properties extends class Hashtable. Figure 19.21 demonstrates several methods of class Properties.
Figure 19.21. Properties class of package java.util.
(This item is displayed on pages 947 - 949 in the print version)
1 // Fig. 19.21: PropertiesTest.java 2 // Demonstrates class Properties of the java.util package. 3 import java.io.FileOutputStream; 4 import java.io.FileInputStream; 5 import java.io.IOException; 6 import java.util.Properties; 7 import java.util.Set; 8 9 public class PropertiesTest 10 { 11 private Properties table; 12 13 // set up GUI to test Properties table 14 public PropertiesTest() 15 { 16 table = new Properties(); // create Properties table 17 18 // set properties 19 table.setProperty( "color", "blue" ); 20 table.setProperty( "width", "200" ); 21 22 System.out.println( "After setting properties" ); 23 listProperties(); // display property values 24 25 // replace property value 26 table.setProperty( "color", "red" ); 27 28 System.out.println( "After replacing properties" ); 29 listProperties(); // display property values 30 31 saveProperties(); // save properties 32 33 table.clear(); // empty table 34 35 System.out.println( "After clearing properties" ); 36 listProperties(); // display property values 37 38 loadProperties(); // load properties 39 40 // get value of property color 41 Object value = table.getProperty( "color" ); 42 43 // check if value is in table 44 if ( value != null ) 45 System.out.printf( "Property color's value is %s ", value ); 46 else 47 System.out.println( "Property color is not in table" ); 48 } // end PropertiesTest constructor 49 50 // save properties to a file 51 public void saveProperties() 52 { 53 // save contents of table 54 try 55 { 56 FileOutputStream output = new FileOutputStream( "props.dat" ); 57 table.store( output, "Sample Properties" ); // save properties 58 output.close(); 59 System.out.println( "After saving properties" ); 60 listProperties(); 61 } // end try 62 catch ( IOException ioException ) 63 { 64 ioException.printStackTrace(); 65 } // end catch 66 } // end method saveProperties 67 68 // load properties from a file 69 public void loadProperties() 70 { 71 // load contents of table 72 try 73 { 74 FileInputStream input = new FileInputStream( "props.dat" ); 75 table.load( input ); // load properties 76 input.close(); 77 System.out.println( "After loading properties" ); 78 listProperties(); // display property values 79 } // end try 80 catch ( IOException ioException ) 81 { 82 ioException.printStackTrace(); 83 } // end catch 84 } // end method loadProperties 85 86 // output property values 87 public void listProperties() 88 { 89 Set< Object > keys = table.keySet(); // get property names 90 91 // output name/value pairs 92 for ( Object key : keys ) 93 { 94 System.out.printf( 95 "%s %s ", key, table.getProperty( ( String ) key ) ); 96 } // end for 97 98 System.out.println(); 99 } // end method listProperties 100 101 public static void main( String args[] ) 102 { 103 new PropertiesTest(); 104 } // end main 105 } // end class PropertiesTest
|
Line 16 uses the no-argument constructor to create an empty Properties table with no default properties. Class Properties also provides an overloaded constructor that receives a reference to a Properties object containing default property values. Lines 19 and 20 each call Properties method setProperty to store a value for the specified key. If the key does not exist in the table, setProperty returns null; otherwise, it returns the previous value for that key.
Line 41 calls Properties method getProperty to locate the value associated with the specified key. If the key is not found in this Properties object, getProperty returns null. An overloaded version of this method receives a second argument that specifies the default value to return if getProperty cannot locate the key.
Line 57 calls Properties method store to save the contents of the Properties object to the OutputStream object specified as the first argument (in this case, FileOutputStream output). The second argument, a String, is a description of the Properties object. Class Properties also provides method list, which takes a PrintStream argument. This method is useful for displaying the list of properties.
Line 75 calls Properties method load to restore the contents of the Properties object from the InputStream specified as the first argument (in this case, a FileInputStream). Line 89 calls Properties method keySet to obtain a Set of the property names. Line 94 obtains the value of a property by passing a key to method getProperty.