Source of PropertiesTest.java


  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\n", 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(); // display property values
 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\t%s\n", 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
106: 
107: /**************************************************************************
108:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
109:  * Pearson Education, Inc. All Rights Reserved.                           *
110:  *                                                                        *
111:  * DISCLAIMER: The authors and publisher of this book have used their     *
112:  * best efforts in preparing the book. These efforts include the          *
113:  * development, research, and testing of the theories and programs        *
114:  * to determine their effectiveness. The authors and publisher make       *
115:  * no warranty of any kind, expressed or implied, with regard to these    *
116:  * programs or to the documentation contained in these books. The authors *
117:  * and publisher shall not be liable in any event for incidental or       *
118:  * consequential damages in connection with, or arising out of, the       *
119:  * furnishing, performance, or use of these programs.                     *
120:  *************************************************************************/