Lab 15: File IO

The goal of this lab is to practice using Java's file I/O facilities.

1. Introduction

In Lab 11, you wrote a program that read in a file, counted the frequency of the words, and printed the result to the screen. However, you did not write the component that read from a file (I provided that part of the code). In this lab you will take your solution from that lab and write the file-reading code yourself and modify the program so that it sends its results to a file instead of to the screen.

2. Set up

Copy your solution and the Pair class from lab 11 to your lab 15 directory.

cp ../lab11/WordCounter.java .
cp ../lab11/Pair.java .

Also copy these files from the class public directory (they are medieval Yuletide songs, taken from Shira Kamen, The Castle of the Holly King).

cp /homeemp/tvandrun/pub/235/wrenfurze .
cp /homeemp/tvandrun/pub/235/appletreewassail .
cp /homeemp/tvandrun/pub/235/cuttywren .
cp /homeemp/tvnadrun/pub/235/hollymerrymen .

3. Reading in

Open your WordCounter.java in xemacs, delete the declaration of the variable that hold the Vector of words, and comment out the rest of the code inside the main method. Then work through the following steps.

Step 1: Put a try/catch around everything in the main method (including the parts you commented out). Catch an IOException. Everying else you do needs to be in the try block.

Step 2: Make a scanner to read in from the file that the user gives at the command line. Notice that (1) the name of the file is on the command line (2) you need to make a FileInputStream that reads from the specified file; (3) you need to make a Scanner that makes read from the FileInputStream. Compile and test (there should be no ouput yet). Remember that your program works like

java WordCounter filename

Step 3: Write a loop that prints each line of the file to the screen (as we did in class yesterday). Compile and test.

Step 4: Modify the loop that you just wrote so that instead of printing each line to the screen, for each line it creates a StringTokenizer that chops the line into words (ignoring punctiation (except apostrophes), spaces, and numbers), and then write an inner loop that prints each word of the line to the screen.

Step 5: Merge what you have from step 4 with your tally algorithm from lab 11. That is, copy your code to declare an initialize the HashMap from lab 11 to someplace before your loops in steps 3 and 4; and in your inner loop in step 4, instead of printing each word to the screen, use the word to populate the tally HashMap.

Step 6: Uncomment your code from lab 11, and delete the code that is no longer necessary. Compile and test. By now it should display the results of the word tally to the screen, like your final product for lab 11.

4. Writing out

Now modify your code so that instead of printing the tally to the screen, it opens a file and prints the result there. The name of the output file should be the same as the input file, but with ".count" appended. For example, if the input file is "cuttywren", the output file should be "cuttywren.count".

To do this, you will need to declare a variable of type PrintWriter, initialize it to an instantiation of the class, and change your System.out.println to use your PrinteWriter instead of System.out.

5. Turn in

Copy the files you modified to

/homestu/jmichalk/labTA/turnInProjects/yourFirstName,yourLastInitial/lab12


Thomas VanDrunen
Last modified: Tue Nov 28 08:26:10 CST 2006