/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package readtextfiletest; /** * * @author jcmt */ // Fig. 17.09: ReadTextFile.java // This program reads a text file and displays each record. import java.io.File; import java.io.FileNotFoundException; import java.lang.IllegalStateException; import java.util.NoSuchElementException; import java.util.Scanner; import java.util.Formatter; //import com.deitel.ch17.AccountRecord; public class ReadTextFile { private Scanner input; WriteTextFile outputdata = new WriteTextFile(); Formatter screenOutput = new Formatter(System.out); // enable user to open file public void openFile() { try { input = new Scanner( new File( "clients.txt" ) ); outputdata.openFile(); } // end try catch ( FileNotFoundException fileNotFoundException ) { System.err.println( "Error opening file." ); System.exit( 1 ); } // end catch } // end method openFile // read record from file public void readRecords() { // object to be written to screen AccountRecord record = new AccountRecord(); screenOutput.format( "%-10s%-12s%-12s%10s\n", "Account", "First Name", "Last Name", "Balance" ); try // read records from file using Scanner object { while ( input.hasNext() ) { record.setAccount( input.nextInt() ); // read account number record.setFirstName( input.next() ); // read first name record.setLastName( input.next() ); // read last name record.setBalance( input.nextDouble() ); // read balance // display record contents screenOutput.format( "%-10d%-12s%-12s%10.2f\n", record.getAccount(), record.getFirstName(), record.getLastName(), record.getBalance() ); outputdata.writeRecord(record); } // end while } // end try catch ( NoSuchElementException elementException ) { System.err.println( "File improperly formed." ); input.close(); System.exit( 1 ); } // end catch catch ( IllegalStateException stateException ) { System.err.println( "Error reading from file." ); System.exit( 1 ); } // end catch } // end method readRecords // close file and terminate application public void closeFile() { if ( input != null ) input.close(); // close file outputdata.closeFile(); } // end method closeFile } // end class ReadTextFile