@@ -0,0 +1,1840 @@
+package chapter15;%0A%0Aimport java.io.FileNotFoundException;%0Aimport java.util.Formatter;%0Aimport java.util.FormatterClosedException;%0Aimport java.util.NoSuchElementException;%0Aimport java.util.Scanner;%0A%0Apublic class CreateTextFile %7B%0A%0A%09private static Formatter output; // outputs text to a file%0A%09%0A%09public static void main(String%5B%5D args) %7B%0A%09%09openFile();%0A%09%09addRecords();%0A%09%09closeFile();%0A%09%7D%0A%09%0A%09// open file clients.txt%0A%09public static void openFile() %7B%0A%09%09try %7B%0A%09%09%09output = new Formatter(%22clients.txt%22); // open the file%0A%09%09%7D catch (SecurityException securityException) %7B%0A%09%09%09System.err.println(%22Write permission denied. Terminating.%22);%0A%09%09%09System.exit(1); // terminate the program%0A%09%09%7D catch (FileNotFoundException fileNotFoundException) %7B%0A%09%09%09System.err.println(%22Error opening file. Terminating.%22);%0A%09%09%09System.exit(1); // terminate the program%0A%09%09%7D%0A%09%7D%0A%09%0A%09// add record to file%0A%09public static void addRecords() %7B%0A%09%09Scanner input = new Scanner(System.in);%0A%09%09System.out.printf(%22%25s%25n%25s%25n? %22, %22Enter account number, first name, last name and balance.%22, %0A%09%09%09%09%09%09%09%09%09%09%22Enter end-of-file indicator to end input.%22);%0A%09%09%0A%09%09while (input.hasNext()) %7B // loop until end-of-file indicator%0A%09%09%09try %7B%0A%09%09%09%09// output new record to file; assumes valid input%0A%09%09%09%09output.format(%22%25d %25s %25s %25.2f%25n%22, input.nextInt(), input.next(), input.next(), input.nextDouble());%0A%09%09%09%7D catch (FormatterClosedException formatterClosedException) %7B%0A%09%09%09%09System.err.println(%22Error writing to file. Terminating.%22);%0A%09%09%09%09break;%0A%09%09%09%7D catch (NoSuchElementException elementException) %7B%0A%09%09%09%09System.err.println(%22Invalid input. Please try again.%22);%0A%09%09%09%09input.nextLine(); //discard input so user can try again%0A%09%09%09%7D%0A%09%09%09%0A%09%09%09System.out.print(%22? %22);%0A%09%09%7D // end while%0A%09%7D // end method addRecords%0A%09%0A%09// close file%0A%09public static void closeFile() %7B%0A%09%09if (output != null)%0A%09%09%09output.close();%0A%09%7D%0A%7D // end class CreateTextFile%0A

Create CreateTextFile.java