Lab 18: File IO

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

1. Introduction

In Lab 14, 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

Make a directory for this lab. Copy your solution and the Pair class from lab 14 to your lab 18 directory.

cp ../lab14/WordCounter.java .
cp ../lab14/Pair.java .

Also copy these files from the class public directory.

cp /homeemp/tvandrun/pub/235/Ex19 .
cp /homeemp/tvandrun/pub/235/Ps132 .
cp /homeemp/tvandrun/pub/235/Rom12 .
cp /homeemp/tvandrun/pub/235/Rev22 .

3. Reading in

Open your WordCounter.java in xemacs, delete the declaration of the variable that holds the ArrayList 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 14. That is, copy your code to declare an initialize the HashMap from lab 14 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 14, 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 14.

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 "Rom12", the output file should be "Rom12.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

Email your files to Joe.


Thomas VanDrunen
Last modified: Mon Apr 23 14:39:58 CDT 2007