import java.io.EOFException; import java.io.FileInputStream; import java.io.IOException; import java.io.ObjectInputStream; public class ReadSequentialFile { private ObjectInputStream input; // enable user to select file to open public void openFile() { try { // open file input = new ObjectInputStream(new FileInputStream( "clients.ser" )); } catch ( IOException ioException ){ System.err.println( "Error opening file." ); } } // read record from file public void readRecords() { AccountRecord record; System.out.printf( "%-10s%-12s%-12s%10s\n", "Account", "First Name", "Last Name", "Balance" ); try { // input the values from the file while (true) { record = ( AccountRecord ) input.readObject(); // display record contents System.out.printf( "%-10d%-12s%-12s%10.2f\n", record.getAccount(), record.getFirstName(), record.getLastName(), record.getBalance() ); } } catch ( EOFException endOfFileException ) { return; // end of file was reached } catch ( ClassNotFoundException classNotFoundException ) { System.err.println( "Unable to create object." ); } catch ( IOException ioException ) { System.err.println("Error during reading from file."); } } // close file and terminate application public void closeFile() { try { // close file and exit if ( input != null ) input.close(); System.exit(0); } catch ( IOException ioException ) { System.err.println("Error closing file."); System.exit(1); } } }