COP 33337 Section U08 Fall 2015 FORMATTING FILES ================ Here are 2 programs that use exceptions: I. The program Main reduces a run of 1 or more blanks to one blnk. package deleteextrablanks; import javax.swing.JOptionPane; import java.util.Scanner; import java.io.*; public class Main { public static void main(String[] args) { // get the name of the input file FileReader reader = null; String input = JOptionPane.showInputDialog( "Enter the name of the input file"); try { reader = new FileReader(input); } catch( IOException e) { System.out.println("Error! File " + input + " cannot be opened for input!"); System.exit(0); } Scanner in = new Scanner( reader); // get the output file FileWriter writer = null; String output = JOptionPane.showInputDialog( "Enter the name of the output file!"); try { writer = new FileWriter(output); } catch( IOException e) { System.out.println("Error! File " + output + " cannot be opened for output!"); System.exit(0); } PrintWriter out = new PrintWriter( writer); // process the input file while (in.hasNextLine()) { String line = in.nextLine(); int count = 0; // holds the current string number // process the line // check for empty line if (line.equals("")) { out.println(); continue; } Scanner tokens = new Scanner(line); // check for leading blank if (line.charAt(0) == ' ') out.print(" "); // process the strings of line while ( tokens.hasNext()) { // get the next token count++; if (count > 1) // print a blank in front out.print(" "); // get and print the next string String current = tokens.next(); out.print(current); } // end inner while // see if there is a blank at the end of the line if (line.charAt(line.length() - 1) == ' ') out.print(" "); out.println(); } // end outer while // close the files out.close(); in.close(); System.exit(0); } } I entered the file U:\billy.txt for input and U:\papa.txt for output. U is the network drive for my computer. The file billy.txt is the one between the ---- lines. ----------------------------------------------------- Ali Baba Papa Bill Geoff the Chef ----------------------------------------------------- The output file papa.txt is the one between the next 2 ----- lines. ----------------------------------------------------- Ali Baba Papa Bill Geoff the Chef ----------------------------------------------------- II. The next program formats the input file to have no more than 50 characters per line and no broken strings. /* * Main.java * * Created on February 24, 2009, 9:36 AM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ // get an input and an output text file from the user and // copy the lines of the output file onto the ouput file // subject to the conditions below: // the words of the input file are copied in order and // are separated by one blank. There are no leading blanks, // the output lines have no more than 50 characters, and no // word is broken. // Each input line in copied onto one or more new output lines. // The blank lines are copied as blank lines package spring09fileformatter; import java.io.FileReader; import java.io.BufferedReader; import java.io.IOException; import java.io.FileWriter; import java.io.PrintWriter; import java.util.Scanner; import javax.swing.JOptionPane; public class Main { public static void main(String[] args) { // print the title System.out.println("Copying a File"); System.out.println("==============\n\n\n"); // get the input file BufferedReader in = getInputFile(); if (in == null) { System.out.println("Error! You did not enter an input file after 3 tries." + "\nThe program is aborted.\n"); System.exit(1); } PrintWriter out = getOutputFile(); if ( out == null) { System.out.println("Error! You did not enter an output file after 3 tries." + "\nThe program is aborted.\n"); // close the input file try { in.close(); } catch( IOException e) { System.out.println("The input file cannot be closed !"); } System.exit(1); } // copy the input file copy(in,out); try { in.close(); } catch( IOException e) { System.out.println("The input file cannot be closed !"); error++; } out.close(); if (error == 0) System.out.println("This program found no problems."); } private static BufferedReader getInputFile() { // give user 3 tries to get the input file BufferedReader inFile = null; String message = "Enter the name of the input file"; for (int i = 0; i < 3; i++) { try { String input = JOptionPane.showInputDialog(message); inFile = new BufferedReader(new FileReader(input)); return inFile; } catch (IOException exception) { message = exception + ".\nTry another input file!"; } catch (NullPointerException e) { message = e + ".\nDon't hit Cancel!\nTry again!"; } } return inFile; } private static PrintWriter getOutputFile() { // give user 3 tries to get the input file PrintWriter outFile = null; String message = "Enter the name of the output file"; for (int i = 0; i < 3; i++) { try { String input = JOptionPane.showInputDialog(message); outFile = new PrintWriter(new FileWriter(input)); return outFile; } catch (IOException exception) { message = exception + ".\nTry another output file!"; } catch (NullPointerException e) { message = e + ".\nDon't hit Cancel!\nTry again!"; } } return outFile; } private static void copy(BufferedReader inFile, PrintWriter outFile) { String inLine = null; try { inLine = inFile.readLine(); while (inLine != null) { processLine(inLine, outFile); inLine = inFile.readLine(); } } catch ( IOException e) { System.out.println( e + " on line " + inLine + "."); error++; } } // copy the string s onto one or more lines of outfile public static void processLine(String s, PrintWriter out) { final int LINE_SIZE = 50; Scanner scan = new Scanner(s); // to get the words int pos = 0; // the position of the cursor while (scan.hasNext()) { // get and process the token String token = scan.next(); // copy it if (pos == 0) { out.print(token); pos = token.length(); } else if (pos + 1 + token.length() <= LINE_SIZE) { out.print(" "); out.print(token); pos = pos + 1 + token.length(); } else // new line { out.println(); out.print(token); pos = token.length(); } } // end while // go to the next line out.println(); } private static int error = 0; } I enterer the file U:\coca.txt, shown between the 2 ---- lines. --------------------------------------------------------------------------------------- Once upon a time there was a poor old man named Pauper John. Pauper John lived in a old shack made of wood with windows covered with animal skins. He had no children and his wife died more than 40 years ago. --------------------------------------------------------------------------------------- I entered U:\alexander.txt for the output file and I got the file between the 2 ---- lines. ----------------------------------------------------------------------- Once upon a time there was a poor old man named Pauper John. Pauper John lived in a old shack made of wood with windows covered with animal skins. He had no children and his wife died more than 40 years ago. ------------------------------------------------------------------------ I also received the following message below as a printout. Copying a File ============== This program found no problems. BUILD SUCCESSFUL (total time: 21 seconds)