Random Access File IO


A Practical File I/O Example: The java.util.Properties Class

There are many classes in the Java Platform API that depend on the InputStream and OutputStream family of classes in some way or another. An example of one such class is the Properties class.

The Properties class provides a convenient way for you to store application properties in two types of plain text file formats: (property_name/value pairs) or xml files (<properties><entry> tags). Example 18.20 gives the code for a class named AppProperties and example 18.21 shows the AppProperties class in action.

Example 18.20: AppProperties.java

image from book
 1     import java.util.*; 2     import java.io.*; 3 4     public class AppProperties extends Properties { 5       /* 6        * Declare default property names and their values 7        */ 8        public static final String PROPERTY_A_NAME = "PROPERTY_A_NAME"; 9        public static final String PROPERTY_B_NAME = "PROPERTY_B_NAME"; 10       public static final String PROPERTY_C_NAME = "PROPERTY_C_NAME"; 11       public static final String DEFAULT_PROPERTIES_FILENAME = "DEFAULT_PROPERTIES_FILENAME"; 12       public static final String PROPERTIES_FILE_HEADER = "PROPERTIES_FILE_HEADER"; 13 14       private static final String PROPERTY_A_VALUE = "Property A's Value"; 15       private static final String PROPERTY_B_VALUE = "Property B's Value"; 16       private static final String PROPERTY_C_VALUE = "Property C's Value"; 17       private static final String DEFAULT_PROPERTIES_FILENAME_VALUE = "app_prop.xml"; 18       private static final String PROPERTIES_FILE_HEADER_VALUE = "Application Properties File"; 19 20       /** 21        *  Constructor 22        */ 23 24       public AppProperties(){ 25          System.out.println("Attempting to read properties file..."); 26          FileInputStream  fis = null; 27          FileOutputStream fos = null; 28           try{ 29             fis = new FileInputStream(DEFAULT_PROPERTIES_FILENAME_VALUE); 30             this.loadFromXML(fis); 31           }catch(Exception e1){ 32              System.out.println("AppProperties: Problem loading properties file."); 33              System.out.println(e1.toString()); 34              System.out.println("Creating new default properties file."); 35              this.setProperty(PROPERTY_A_NAME, PROPERTY_A_VALUE); 36              this.setProperty(PROPERTY_B_NAME, PROPERTY_B_VALUE); 37              this.setProperty(PROPERTY_C_NAME, PROPERTY_C_VALUE); 38              this.setProperty(DEFAULT_PROPERTIES_FILENAME, DEFAULT_PROPERTIES_FILENAME_VALUE); 39              this.setProperty(PROPERTIES_FILE_HEADER, PROPERTIES_FILE_HEADER_VALUE); 40              try{ 41                  fos = new FileOutputStream(DEFAULT_PROPERTIES_FILENAME_VALUE); 42                  this.storeToXML(fos, PROPERTIES_FILE_HEADER_VALUE); 43              }catch(Exception e2){ 44                  System.out.println("AppProperties: Problem creating default properties file."); 45                  System.out.println(e2.toString()); 46              }finally{ 47                  if(fis != null){ 48                      try{ 49                           fis.close(); 50                      }catch(Exception ignored){ } 51                  } 52 53                  if(fos != null){ 54                      try{ 55                        fos.close(); 56                      }catch(Exception ignored){ } 57                  } 58 59              }// end finally 60          }// end primary try/catch block 61      }// end constructor 62    }// end AppProperties class definition
image from book

Example 18.21: PropertiesTesterApp.java

image from book
 1     public class PropertiesTesterApp { 2       public static void main(String[] args){ 3         AppProperties properties = new AppProperties(); 4         System.out.println(properties.getProperty(properties.PROPERTY_A_NAME)); 5       } 6     }
image from book

Referring to example 18-20 — the AppProperties class extends the java.util.Properties class. The Properties class stores properties in a HashMap. A property consists of a property name (property key) and a property value. These name/value pairs are inserted into a Properties object’s HashMap for later retrieval and use in a program. As I mentioned above, a Properties object can persist its collection of property name/value pairs to disk in two plain text formats: 1) name/value pairs or 2) xml tagged entries. The AppProperties class defines several default application properties: three bogus properties for example purposes and two real properties. The two real properties include the default name of the application’s property file and a default properties file header string.

When an AppProperties object is created, an attempt is made to open and read the default properties file. If the open is successful the properties file is read and the AppProperties object’s HashMap is initialized. (This HashMap initialization takes place automatically.) If, however, the file does not exist or cannot be opened, the AppProperties object initializes its HashMap to a set of default properties and then attempts to create the properties file and save those properties to the file. This example creates an xml tagged properties file by using the storeToXML() method.

Referring to example 18.21 — the PropertiesTesterApp simply creates an instance of an AppProperties object. Once the object is created the getProperty() method is called to retrieve the value of the property named PROPERTY_A_NAME.

Figure 18-25 shows the PropertiesTesterApp program being executed for the first time. (i.e., the app_prop.xml file does not initially exist) Figure 18-26 shows the contents of the app_prop.xml file after the program executes.

image from book
Figure 18-25: Initial Execution of PropertiesTesterApp (Example 18.21)

image from book
Figure 18-26: Contents of app_prop.xml After Example 18.21 Executes

Quick Review

The Properties class is an example of a Java API class that depends on the services of the InputStream and OutputStream classes to persist property data to a file.




Java For Artists(c) The Art, Philosophy, and Science of Object-Oriented Programming
Java For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504052
EAN: 2147483647
Year: 2007
Pages: 452

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net