CSCI 201 -- I/O and Exceptions

In this lab we will look at some of Java's I/O classes and see how they must be used with Java's exception handling mechanism.

Downloading the project framework

Download, a ZIP file containing a NetBeans project named IOExcept and unZIP this project into your csci/201 directory. Try to make your Projects panel look something like the following picture before continuing.
Initial IOExcept Projects Panel

Where to start working

All the Java code you will use in this lab is contained in the file If you look down into this file, you will see the following three comments.

    // ***********************************************
    // You should not change anything before this line
    // ***********************************************

Do what the line says.

What the program does

Getting ready to read

Now let's return to Using Java I/O isn't easy. Take a careful look at the code within the main method of IOExcept. The body of main is one big try block. This try block is used to catch the exception FileNotFoundException that could occur when we call the FileInput Stream constructor.

    InputStream inStream = new FileInputStream(new File(inName)) ;

In our case, the constructor should alway succeed because it is opening a system-wide file /usr/local/csci/201/iolabin.txt which has been created for this lab.

The FileInputStream object is created so that it can be used to construct a Scanner object for reading the opened file.

    Scanner stdin = new Scanner(inStream) ;


With stdin, our Scanner object, we can begin to read the file in a while loop. First we initialize a couple of variables, LineNum and fileTotal, and then we execute our loop to read the lines of the file.

    while (stdin.hasNext()) {
        // Read a line and increment line number count
        String line = stdin.nextLine() ;
        ++LineNum ;
        System.out.println("Line #" + LineNum + ": " + line) ;

But we're still not there. We want to read our file one line at a time and only process lines that consist of "good" data. To accomplish this goal our program creates a new scanner for each line of input and then enters a loop that separately prints each word of the line.

Go ahead and run the program. You'll see that it prints and enumerates the words of each line. Study the code to make sure you understand how this is done.

The goal

Right now we have a program that prints out the "words" in a file. Some of those words are numbers and some aren't. We want to modify the program so that it adds up all the numbers, but only on lines that are composed solely of numbers! Consequently, the total for the following input data should be 3305 because the 100 and 200 occur on a line that contains a non-integer and consequently are ignored.

   202 333
   255 310
   java rules
   100 CSCI 200


If we knew every word in the file was a number, we could read these numbers simply by invoking the nextInt method of the Scanner class. However, if nextInt is used when the next word is not an integer, the Java system will raise the InputMismatchException exception. The key to solving our problem is to catch InputMismatchException in such a way that the sum does not change.

For your first modification of IOExcept, make your program sum up all the numbers of the file that occur on lines composed solely of integers. There are two variables in the present program that don't seem to do much. We suggest you use them in solving this part of the lab. Use lineTotal to sum up all the numbers on a signle line, and use fileTotal to sum up all the numbers on good lines within the file. If an InputMismatchException occurs while adding up the numbers within a line, do not add lineTotal to fileTotal

Show the instructor your program to add the numbers on the good lines.

Your second, and final, task is to modify your program so that is creates a file called iolabsum.txt within your csci/201 directory and prints the sum to that file.

Doing this isn't very hard. First, you create a PrintStream object for the new file. Then you need to actually print fileTotal using the println method of your new PrintStream object. It's two lines of code.

Show the instructor your program to create and print to iolabsum.txt.