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.
Introduction to Computers, the Internet and the World Wide Web
Introduction to Java Applications
Introduction to Classes and Objects
Control Statements: Part I
Control Statements: Part 2
Methods: A Deeper Look
Arrays
Classes and Objects: A Deeper Look
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Polymorphism
GUI Components: Part 1
Graphics and Java 2D™
Exception Handling
Files and Streams
Recursion
Searching and Sorting
Data Structures
Generics
Collections
Introduction to Java Applets
Multimedia: Applets and Applications
GUI Components: Part 2
Multithreading
Networking
Accessing Databases with JDBC
Servlets
JavaServer Pages (JSP)
Formatted Output
Strings, Characters and Regular Expressions
Appendix A. Operator Precedence Chart
Appendix B. ASCII Character Set
Appendix C. Keywords and Reserved Words
Appendix D. Primitive Types
Appendix E. (On CD) Number Systems
Appendix F. (On CD) Unicode®
Appendix G. Using the Java API Documentation
Appendix H. (On CD) Creating Documentation with javadoc
Appendix I. (On CD) Bit Manipulation
Appendix J. (On CD) ATM Case Study Code
Appendix K. (On CD) Labeled break and continue Statements
Appendix L. (On CD) UML 2: Additional Diagram Types
Appendix M. (On CD) Design Patterns
Appendix N. Using the Debugger
Inside Back Cover