import java.io.*; class Date implements Serializable { public Date( ) { this( 1, 1, 2000 ); } public Date( int m, int d, int y ) { month = m; date = d; year = y; if( !isValid( ) ) // really should throw exception... { month = date = 1; year = 2000; } } public String toString( ) { return month + "/" + date + "/" + year; } // Not a real implementation private boolean isValid( ) { return month >= 1 && month <= 12 && date >= 1 && date <= 31; } private void readObject( ObjectInputStream in ) throws IOException, ClassNotFoundException { in.defaultReadObject( ); if( !isValid( ) ) throw new IOException( "Corrupted date" ); } private int month; private int date; private int year; } class Person implements Serializable { public static final long serialVersionUID = -2165050113482428735L; public Person( String n, Date b, Person m, Person f ) { name = n; birthDate = b; mother = m; father = f; } public Person getFather( ) { return father; } public Person getMother( ) { return mother; } public String getName( ) { return name; } public Date getBirthDate( ) { return birthDate; } public String toString( ) { return "[" + name + " born on=" + birthDate + "(mother=" + getMother( ).getName( ) + ") (father=" + getFather( ).getName( ) + ")" + "]"; } private void readObject( ObjectInputStream in ) throws IOException, ClassNotFoundException { ObjectInputStream.GetField gf = in.readFields( ); name = (String) gf.get( "name", null ); birthDate = (Date) gf.get( "birthDate", null ); Person [] parents = null; try { parents = (Person[]) gf.get( "parents", null ); } catch( IllegalArgumentException e ) { } if( parents != null ) // old format { mother = parents[ 0 ]; father = parents[ 1 ]; } else { mother = (Person) gf.get( "mother", null ); father = (Person) gf.get( "father", null ); } } public static final Person UNKNOWN = new Person( "????", new Date( ), null, null ); private String name; private Date birthDate; private Person mother; private Person father; } class Example3 { public static void printPersons( Person [] arr ) { for( int i = 0; i < arr.length; i++ ) System.out.println( arr[ i ] ); } public static void main( String[] args ) { ObjectInputStream is = null; Person [] p = null; try { is = new ObjectInputStream( new FileInputStream( "ex3.ser" ) ); p = (Person[]) is.readObject( ); System.out.println( "****READING FROM ex3****" ); printPersons( p ); System.out.println( "Persons 0 and 4 same? " + ( p[ 0 ] == p[ 4 ] ) ); System.out.println( "Birthday 2 and 3 same? " + ( p[ 2 ].getBirthDate( ) == p[ 3 ].getBirthDate( ) ) ); is.close( ); } catch( ClassNotFoundException e ) { System.err.println( "What's a Person? " + e ); } catch( IOException e ) { System.err.println( "I/O problem: " + e ); } finally { try { if( is != null ) is.close( ); } catch( IOException e ) { System.err.println( "Can't close: " + e ); } } // Write out to ex3a in new format ObjectOutputStream os = null; try { os = new ObjectOutputStream( new FileOutputStream( "ex3a.ser" ) ); os.writeObject( p ); } catch( IOException e ) { System.err.println( "I/O problem: " + e ); return; } finally { try { if( os != null ) os.close( ); } catch( IOException e ) { System.err.println( "Can't close: " + e ); } } // Read back from ex3a try { is = new ObjectInputStream( new FileInputStream( "ex3a.ser" ) ); Person [] people2 = (Person[]) is.readObject( ); System.out.println( "****READING FROM ex3a****" ); printPersons( people2 ); System.out.println( "Persons 0 and 4 same? " + ( people2[ 0 ] == people2[ 4 ] ) ); System.out.println( "Birthday 2 and 3 same? " + ( people2[ 2 ].getBirthDate( ) == people2[ 3 ].getBirthDate( ) ) ); is.close( ); } catch( ClassNotFoundException e ) { System.err.println( "What's a Person? " + e ); } catch( IOException e ) { System.err.println( "I/O problem: " + e ); } finally { try { if( is != null ) is.close( ); } catch( IOException e ) { System.err.println( "Can't close: " + e ); } } } }