// Fig. 14.11: 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.applet.AudioClip; public class readingRecord{ private AudioClip sound1, currentSound; private Scanner input; // enable user to open file public void openFile() { try { input = new Scanner( new File( "people.txt" ) ); } // end try catch ( FileNotFoundException fileNotFoundException ) { System.err.println( "Error" ); System.exit( 1 ); } // end catch } // end method openFile // read record from file public void readRecords() { // object to be written to screen USBRecord record = new USBRecord(); System.out.printf( "%-10s"); //%-12s%-12s%10s\n try // read records from file using Scanner object { while ( input.hasNext() ) { record.setName( input.nextInt() ); // read Name //record.setFirstName( input.next() ); // read first name //record.setLastName( input.next() ); // read last name //record.setBalance( input.nextDouble() ); // read balance // display record contents sound1 = getAudioClip(getDocumentBase(), "pass-in_normal.wav"); currentSound = sound1; //System.out.printf( "%-10d", // record.getName() ); //, record.getFirstName(), //record.getLastName(), record.getBalance() ); } // end while //%-12s%-12s%10.2f\n } // 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 } // end method closeFile } // end class ReadTextFile /************************************************************************* * (C) Copyright 1992-2007 by Deitel & Associates, Inc. and * * Pearson Education, Inc. All Rights Reserved. * * * * DISCLAIMER: The authors and publisher of this book have used their * * best efforts in preparing the book. These efforts include the * * development, research, and testing of the theories and programs * * to determine their effectiveness. The authors and publisher make * * no warranty of any kind, expressed or implied, with regard to these * * programs or to the documentation contained in these books. The authors * * and publisher shall not be liable in any event for incidental or * * consequential damages in connection with, or arising out of, the * * furnishing, performance, or use of these programs. * *************************************************************************/