Source of WriteRandomFile.java


  1: // Fig. 14.26: WriteRandomFile.java
  2: // This program retrieves information from the user at the 
  3: // keyboard and writes the information to a random-access file.
  4: import java.io.File;
  5: import java.io.IOException;
  6: import java.io.RandomAccessFile;
  7: import java.util.NoSuchElementException;
  8: import java.util.Scanner;
  9: 
 10: import com.deitel.jhtp6.ch14.RandomAccessAccountRecord;
 11: 
 12: public class WriteRandomFile
 13: {  
 14:    private RandomAccessFile output;
 15:    
 16:    private static final int NUMBER_RECORDS = 100;
 17: 
 18:    // enable user to choose file to open
 19:    public void openFile()
 20:    {
 21:       try // open file
 22:       {
 23:          output = new RandomAccessFile( "clients.dat", "rw" );
 24:       } // end try
 25:       catch ( IOException ioException )
 26:       {
 27:          System.err.println( "File does not exist." );
 28:       } // end catch
 29:    } // end method openFile
 30: 
 31:    // close file and terminate application
 32:    public void closeFile() 
 33:    {
 34:       try // close file and exit
 35:       {
 36:          if ( output != null )
 37:             output.close();
 38:       } // end try
 39:       catch ( IOException ioException )
 40:       {
 41:          System.err.println( "Error closing file." );
 42:          System.exit( 1 );
 43:       } // end catch
 44:    } // end method closeFile
 45: 
 46:    // add records to file
 47:    public void addRecords()
 48:    {
 49:       // object to be written to file
 50:       RandomAccessAccountRecord record = new RandomAccessAccountRecord();
 51: 
 52:       int accountNumber = 0; // account number for AccountRecord object
 53:       String firstName; // first name for AccountRecord object
 54:       String lastName; // last name for AccountRecord object
 55:       double balance; // balance for AccountRecord object
 56: 
 57:       Scanner input = new Scanner( System.in );
 58: 
 59:       System.out.printf( "%s\n%s\n%s\n%s\n\n",
 60:          "To terminate input, type the end-of-file indicator ",
 61:          "when you are prompted to enter input.",
 62:          "On UNIX/Linux/Mac OS X type <ctrl> d then press Enter",
 63:          "On Windows type <ctrl> z then press Enter" );
 64: 
 65:       System.out.printf( "%s %s\n%s", "Enter account number (1-100),",
 66:          "first name, last name and balance.", "? " );
 67: 
 68:       while ( input.hasNext() ) // loop until end-of-file indicator
 69:       {
 70:          try // output values to file
 71:          {
 72:             accountNumber = input.nextInt(); // read account number
 73:             firstName = input.next(); // read first name
 74:             lastName = input.next(); // read last name
 75:             balance = input.nextDouble(); // read balance
 76: 
 77:             if ( accountNumber > 0 && accountNumber <= NUMBER_RECORDS )
 78:             {
 79:                record.setAccount( accountNumber );
 80:                record.setFirstName( firstName );
 81:                record.setLastName( lastName );
 82:                record.setBalance( balance );
 83: 
 84:                output.seek( ( accountNumber - 1 ) * // position to proper
 85:                   RandomAccessAccountRecord.SIZE ); // location for file
 86:                record.write( output );
 87:             } // end if
 88:             else
 89:                System.out.println( "Account must be between 0 and 100." );
 90:          } // end try
 91:          catch ( IOException ioException )
 92:          {
 93:             System.err.println( "Error writing to file." );
 94:             return;
 95:          } // end catch
 96:          catch ( NoSuchElementException elementException )
 97:          {
 98:             System.err.println( "Invalid input. Please try again." );
 99:             input.nextLine(); // discard input so user can try again
100:          } // end catch
101: 
102:          System.out.printf( "%s %s\n%s", "Enter account number (1-100),",
103:             "first name, last name and balance.", "? " );
104:       } // end while
105:    } // end method addRecords
106: } // end class WriteRandomFile
107: 
108: /*************************************************************************
109: * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
110: * Pearson Education, Inc. All Rights Reserved.                           *
111: *                                                                        *
112: * DISCLAIMER: The authors and publisher of this book have used their     *
113: * best efforts in preparing the book. These efforts include the          *
114: * development, research, and testing of the theories and programs        *
115: * to determine their effectiveness. The authors and publisher make       *
116: * no warranty of any kind, expressed or implied, with regard to these    *
117: * programs or to the documentation contained in these books. The authors *
118: * and publisher shall not be liable in any event for incidental or       *
119: * consequential damages in connection with, or arising out of, the       *
120: * furnishing, performance, or use of these programs.                     *
121: *************************************************************************/