
public class CreateTextFile
{
// enable user to open file
public void openFile()
{
try
{ Formatter output = new Formatter( "clients.txt" );
} // 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
41
42
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( )// loop until end-of-file indicator
55 {
56 try // output values to file
57 {
58 // retrieve data to be output

   record.setBalance( input.nextDouble() );
 if ( record.getAccount() > 0 )
 {
 // write new record



 } // end if
 else
 {
 System.out.println(
 "Account number must be greater than 0." );
 } // end else
 } // end try
 catch ( FormatterClosedException formatterClosedException )
 {
 System.err.println( "Error writing to file." );
 return;
 } // end catch
 catch ( NoSuchElementException elementException )
 {
 System.err.println( "Invalid input. Please try again." );
 input.nextLine(); // discard input so user can try again
 } // end catch