Source of CreateTextFile.java


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