Source of CreateSequentialFile.java


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