1  // File: ReadSequentialFile.java (Fig. 15.6 in JHTP2)
  2  // This program reads a file sequentially and displays each
  3  // record in text fields.
  4  
  5  import java.io.*;
  6  import java.awt.*;
  7  import java.awt.event.*;
  8  
  9  public class ReadSequentialFile extends Frame
 10                                  implements ActionListener {
 11  
 12     // TextFields to display account number, first name,
 13     // last name and balance.
 14     private TextField accountField, firstNameField,
 15                       lastNameField, balanceField;
 16  
 17     private Button next,   // get next record in file
 18                    done;   // quit program
 19  
 20     // Application other pieces
 21     private DataInputStream input;
 22  
 23     // Constructor -- initialize the Frame 
 24     public ReadSequentialFile()
 25     {
 26        super( "Read Client File" );
 27  
 28        // Open the file
 29        try {
 30           input = new DataInputStream(
 31                       new FileInputStream( "client.dat" ) );
 32        }
 33        catch ( IOException e ) {
 34           System.err.println( "File not opened properly\n" +
 35                               e.toString() );
 36           System.exit( 1 );
 37        }      
 38  
 39        setSize( 300, 150 );
 40        setLayout( new GridLayout( 5, 2 ) );
 41  
 42        // create the components of the Frame
 43        add( new Label( "Account Number" ) );
 44        accountField = new TextField();
 45        accountField.setEditable( false );
 46        add( accountField );
 47  
 48        add( new Label( "First Name" ) );
 49        firstNameField = new TextField( 20 );
 50        firstNameField.setEditable( false );
 51        add( firstNameField );      
 52  
 53        add( new Label( "Last Name" ) );
 54        lastNameField = new TextField( 20 );
 55        lastNameField.setEditable( false );
 56        add( lastNameField );
 57  
 58        add( new Label( "Balance" ) );
 59        balanceField = new TextField( 20 );
 60        balanceField.setEditable( false );
 61        add( balanceField );
 62  
 63        next = new Button( "Next" );
 64        next.addActionListener( this );
 65        add( next );      
 66  
 67        done = new Button( "Done" );
 68        done.addActionListener( this );
 69        add( done );       
 70  
 71        setVisible( true );  
 72     }
 73  
 74     public void actionPerformed( ActionEvent e )
 75     {
 76        if ( e.getSource() == next )
 77           readRecord();
 78        else
 79           closeFile();
 80     }
 81  
 82     public void readRecord()
 83     {
 84        int account;
 85        String first, last;
 86        double balance;
 87  
 88        // input the values from the file
 89        try {
 90           account = input.readInt();
 91           first = input.readUTF();
 92           last = input.readUTF();
 93           balance = input.readDouble();
 94  
 95           accountField.setText( String.valueOf( account ) );
 96           firstNameField.setText( first );
 97           lastNameField.setText( last );
 98           balanceField.setText( String.valueOf( balance ) );
 99        }
100        catch ( EOFException eof ) {
101           closeFile();
102        }
103        catch ( IOException e ) {
104           System.err.println( "Error during read from file\n" +
105                               e.toString() );
106           System.exit( 1 );
107        }
108     }
109  
110     private void closeFile()
111     {
112        try {
113           input.close();
114           System.exit( 0 );
115        }
116        catch ( IOException e ) {
117           System.err.println( "Error closing file\n" +
118                               e.toString() );
119           System.exit( 1 );
120        }
121     }
122  
123     public static void main( String args[] )
124     {
125        new ReadSequentialFile();
126     }
127  }
128