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 Person( String n, Date b, Person mother, Person father ) { name = n; birthDate = b; parents = new Person[] { mother, father }; } public Person getFather( ) { return parents[ 1 ]; } public Person getMother( ) { return parents[ 0 ]; } public String getName( ) { return name; } public Date getBirthDate( ) { return birthDate; } public String toString( ) { return "[" + name + " born on=" + birthDate + "(mother=" + getMother( ).getName( ) + ") (father=" + getFather( ).getName( ) + ")" + "]"; } public static final Person UNKNOWN = new Person( "????", new Date( ), null, null ); private String name; private Date birthDate; private Person [] parents; } class Example2 { public static Person [] createArray( ) { Person [] p = new Person[ 5 ]; Date twinBday = new Date( 10, 2, 1950 ); p[ 0 ] = new Person( "Joe", new Date( 3, 4, 1930 ), Person.UNKNOWN, Person.UNKNOWN ); p[ 1 ] = new Person( "Sue", new Date( 5, 5, 1932 ), Person.UNKNOWN, Person.UNKNOWN ); p[ 2 ] = new Person( "Bobby", twinBday, p[ 1 ], p[ 0 ] ); p[ 3 ] = new Person( "Sue", twinBday, p[ 1 ], p[ 0 ] ); p[ 4 ] = p[ 0 ]; return p; } public static void printPersons( Person [] arr ) { for( int i = 0; i < arr.length; i++ ) System.out.println( arr[ i ] ); } public static void corruptData( Person [] p ) { try { Date d1 = p[ 0 ].getBirthDate( ); java.lang.reflect.Field f = Date.class.getDeclaredField( "month" ); f.setAccessible( true ); f.setInt( d1, -9999 ); } catch( NoSuchFieldException e ) { e.printStackTrace( ); } catch( IllegalAccessException e ) { e.printStackTrace( ); } } public static void main( String[] args ) { // Write out some stuff ObjectOutputStream os1 = null; ObjectOutputStream os2 = null; try { os1 = new ObjectOutputStream( new FileOutputStream( "ex2.ser" ) ); os2 = new ObjectOutputStream( new FileOutputStream( "ex3.ser" ) ); Person [] people1 = createArray( ); os2.writeObject( people1 ); // Clean data for example #3 // Simulate someone eventually corrupting data on disk for example #2 // Could do with hex editor after the write corruptData( people1 ); System.out.println( "Here is what is now actually on the disk for ex #2: " ); printPersons( people1 ); os1.writeObject( people1 ); } catch( IOException e ) { System.err.println( "I/O problem: " + e ); return; } finally { try { if( os1 != null ) os1.close( ); } catch( IOException e ) { System.err.println( "Can't close: " + e ); } } // Try to read it back in ObjectInputStream is = null; try { is = new ObjectInputStream( new FileInputStream( "ex2.ser" ) ); System.out.println( "Reading from disk" ); Person [] people2 = (Person[]) is.readObject( ); System.out.println( "About to print people2" ); 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 ); } } } }