public class ReadSequentialFile
1: // Fig. 14.20: ReadSequentialFile.java
2: // This program reads a file of objects sequentially
3: // and displays each record.
4: import java.io.EOFException;
5: import java.io.FileInputStream;
6: import java.io.IOException;
7: import java.io.ObjectInputStream;
8:
9: import com.deitel.jhtp6.ch14.AccountRecordSerializable;
10:
11: public class ReadSequentialFile
12: {
13: private ObjectInputStream input;
14:
15: // enable user to select file to open
16: public void openFile()
17: {
18: try // open file
19: {
20: input = new ObjectInputStream(
21: new FileInputStream( "clients.ser" ) );
22: } // end try
23: catch ( IOException ioException )
24: {
25: System.err.println( "Error opening file." );
26: } // end catch
27: } // end method openFile
28:
29: // read record from file
30: public void readRecords()
31: {
32: AccountRecordSerializable record;
33: System.out.printf( "%-10s%-12s%-12s%10s\n", "Account",
34: "First Name", "Last Name", "Balance" );
35:
36: try // input the values from the file
37: {
38: while ( true )
39: {
40: record = ( AccountRecordSerializable ) input.readObject();
41:
42: // display record contents
43: System.out.printf( "%-10d%-12s%-12s%10.2f\n",
44: record.getAccount(), record.getFirstName(),
45: record.getLastName(), record.getBalance() );
46: } // end while
47: } // end try
48: catch ( EOFException endOfFileException )
49: {
50: return; // end of file was reached
51: } // end catch
52: catch ( ClassNotFoundException classNotFoundException )
53: {
54: System.err.println( "Unable to create object." );
55: } // end catch
56: catch ( IOException ioException )
57: {
58: System.err.println( "Error during reading from file." );
59: } // end catch
60: } // end method readRecords
61:
62: // close file and terminate application
63: public void closeFile()
64: {
65: try // close file and exit
66: {
67: if ( input != null )
68: input.close();
69: System.exit( 0 );
70: } // end try
71: catch ( IOException ioException )
72: {
73: System.err.println( "Error closing file." );
74: System.exit( 1 );
75: } // end catch
76: } // end method closeFile
77: } // end class ReadSequentialFile
78:
79: /*************************************************************************
80: * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
81: * Pearson Education, Inc. All Rights Reserved. *
82: * *
83: * DISCLAIMER: The authors and publisher of this book have used their *
84: * best efforts in preparing the book. These efforts include the *
85: * development, research, and testing of the theories and programs *
86: * to determine their effectiveness. The authors and publisher make *
87: * no warranty of any kind, expressed or implied, with regard to these *
88: * programs or to the documentation contained in these books. The authors *
89: * and publisher shall not be liable in any event for incidental or *
90: * consequential damages in connection with, or arising out of, the *
91: * furnishing, performance, or use of these programs. *
92: *************************************************************************/