Reading and Writing Your Own Objects

   

You can also customize your objects to have the capability to be serialized just as you did with the Date class.

Listing 22.4 shows the source code for serializing an example class called Employee. Because all the data within the class can be serialized, all you had to do in this case was to implement the java.io.Serializable interface.

Listing 22.4 Source Code for Employee.java
 public class Employee implements java.io.Serializable {   // Private instance variables for an employee   private String firstName = null;   private String lastName = null;   private float payRate;   private int hoursWorked;   // Constructor   public Employee( String first, String last, float rate, int hours )   {     super();     firstName = first;     lastName = last;     payRate = rate;     hoursWorked = hours;   }   // Override the toString() method from object to display an employee   public String toString()   {     StringBuffer strBuf = new StringBuffer( "First: " );     strBuf.append( getFirstName() );     strBuf.append( " Last: " );     strBuf.append( getLastName() );     strBuf.append( " Rate: " );     strBuf.append( getPayRate() );     strBuf.append( " Hours: " );     strBuf.append( getHoursWorked() );     return strBuf.toString();   }   // Public Getters for the object's state   public String getFirstName()   {     return firstName;   }   public String getLastName()   {     return lastName;   }   public float getPayRate()   {     return payRate;   }   public int getHoursWorked()   {     return hoursWorked;   } } 

A toString() method, which is actually inherited from the Object class, was implemented in the Employee class. This provides an easy way to display an instance of the Employee as a string.

Listing 22.5 shows the source code for a class to test the Employee class shown previously.

Listing 22.5 Source Code for EmployeeTest
 import java.io.*; public class EmployeeTest {   // Used to keep from hard-coding the filename in multiple places   public static final String EMPLOYEE_FILE = "employees.dat";   public static void main(String[] args)   {     // Create a few employees     Employee employee1 = new Employee( "Zachary", "Cavaness", 10.25f, 40 );     Employee employee2 = new Employee( "Joshua", "Cavaness", 10.25f, 40 );     Employee employee3 =  new Employee( "Tracy", "Cavaness", 21.50f, 40 );     try     {       // Create the File and Object streams to serialize to the file       FileOutputStream out =  new FileOutputStream( EmployeeTest.EMPLOYEE_FILE ); ObjectOutputStream objOutStr = new ObjectOutputStream( out );       objOutStr.writeObject( employee1 );       objOutStr.writeObject( employee2 );       objOutStr.writeObject( employee3 );       // close the open resources       objOutStr.close();       out.close();     }     catch( FileNotFoundException ex )     {       System.out.println( "Could not create the employee file" );       System.exit(-1);     }     catch( IOException ex )     {       System.out.println( "Could not serialize the employees" );       System.exit(-1);     }     // Read the employees back in     try     {       Object obj = null;       // Create the file and object streams       FileInputStream in = new FileInputStream( EmployeeTest.EMPLOYEE_FILE );       ObjectInputStream objInStr = new ObjectInputStream( in );       // In this example, you keep going until the EOFException is       // raised and then you catch that exception and stop while( true )       {         obj = objInStr.readObject();         System.out.println( obj );       }     }     catch( EOFException ex )     {       System.out.println( "All records have been read in" );     }     catch( FileNotFoundException ex )     {       System.out.println( " Could not open the file " + EmployeeTest.EMPLOYEE_FILE );     }     catch( IOException ex )     {       System.out.println( "Could not serialize the employees" );     }     catch( ClassNotFoundException ex )     {       System.out.println(  "Could not find the class definition for serialized class" ); }   } } 

The EmployeeTest class is used to test the serialization of the Employee class from Listing 22.4. It performs a similar process as in the OurDateUtility class from before. It serializes objects using file and object streams. Here's the output from running the EmployeeTest class:

 C:\jdk1.3se_book\classes>java EmployeeTest First: Zachary Last: Cavaness Rate: 10.25 Hours: 40 First: Joshua Last: Cavaness Rate: 10.25 Hours: 40 First: Tracy Last: Cavaness Rate: 21.5 Hours: 40 All records have been read in C:\jdk1.3se_book\classes> 
   


Special Edition Using Java 2 Standard Edition
Special Edition Using Java 2, Standard Edition (Special Edition Using...)
ISBN: 0789724685
EAN: 2147483647
Year: 1999
Pages: 353

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