public class ReadTextFile
1: // Fig. 14.11: ReadTextFile.java
2: // This program reads a text file and displays each record.
3: import java.io.File;
4: import java.io.FileNotFoundException;
5: import java.lang.IllegalStateException;
6: import java.util.NoSuchElementException;
7: import java.util.Scanner;
8:
9: import com.deitel.jhtp6.ch14.AccountRecord;
10:
11: public class ReadTextFile
12: {
13: private Scanner input;
14:
15: // enable user to open file
16: public void openFile()
17: {
18: try
19: {
20: input = new Scanner( new File( "clients.txt" ) );
21: } // end try
22: catch ( FileNotFoundException fileNotFoundException )
23: {
24: System.err.println( "Error opening file." );
25: System.exit( 1 );
26: } // end catch
27: } // end method openFile
28:
29: // read record from file
30: public void readRecords()
31: {
32: // object to be written to screen
33: AccountRecord record = new AccountRecord();
34:
35: System.out.printf( "%-10s%-12s%-12s%10s\n", "Account",
36: "First Name", "Last Name", "Balance" );
37:
38: try // read records from file using Scanner object
39: {
40: while ( input.hasNext() )
41: {
42: record.setAccount( input.nextInt() ); // read account number
43: record.setFirstName( input.next() ); // read first name
44: record.setLastName( input.next() ); // read last name
45: record.setBalance( input.nextDouble() ); // read balance
46:
47: // display record contents
48: System.out.printf( "%-10d%-12s%-12s%10.2f\n",
49: record.getAccount(), record.getFirstName(),
50: record.getLastName(), record.getBalance() );
51: } // end while
52: } // end try
53: catch ( NoSuchElementException elementException )
54: {
55: System.err.println( "File improperly formed." );
56: input.close();
57: System.exit( 1 );
58: } // end catch
59: catch ( IllegalStateException stateException )
60: {
61: System.err.println( "Error reading from file." );
62: System.exit( 1 );
63: } // end catch
64: } // end method readRecords
65:
66: // close file and terminate application
67: public void closeFile()
68: {
69: if ( input != null )
70: input.close(); // close file
71: } // end method closeFile
72: } // end class ReadTextFile
73:
74: /*************************************************************************
75: * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
76: * Pearson Education, Inc. All Rights Reserved. *
77: * *
78: * DISCLAIMER: The authors and publisher of this book have used their *
79: * best efforts in preparing the book. These efforts include the *
80: * development, research, and testing of the theories and programs *
81: * to determine their effectiveness. The authors and publisher make *
82: * no warranty of any kind, expressed or implied, with regard to these *
83: * programs or to the documentation contained in these books. The authors *
84: * and publisher shall not be liable in any event for incidental or *
85: * consequential damages in connection with, or arising out of, the *
86: * furnishing, performance, or use of these programs. *
87: *************************************************************************/