CSCI 160 Lecture 13 page 3

Text Files (continued) - Input part 2

The process of creating an object of type BufferedReader as described previously properly opens the file for input as part of the execution of the instructor - thus filling the buffer and connecting the buffer and file name. Suppose InFile is the name of the BufferedReader so created.

To actually read from a text file, we use the readLine method. Normally this is done by specifying a variable of type string and, then, readLineing into that string. However, readLine can raise an I/O exception so you must prepare for that exception as follows:

String S;
...
try
{
        S=InFile.readLine();
}
catch(IOException e)
{
        whatever you do for the exception goes here
}

The net effect of these lines is to read the next line of data from the file associated with InFile unless an exception occurs. A line of data is any line in the file ended by an end of line character (a line feed in Unix or a carriage return and line feed in DOS/Windows) or by an end of file character for the last line in the file except when the program has already read all the data in the file and is at the end of file. In this latter case, S gets the value null which represents a null object of any kind (and is different from an empty string).

Thus, the following program segment will read all the data from file file1.txt and print it on the CS160Window MyWindow:

try
{
        InFile=new BufferedReader(new FileReader("file1.txt"));
}
catch(FileNotFoundException e)
{
        System.out.println("ERROR: "+e);
        System.exit(0);
}

try
{
        String S=InFile.readLine();
}
catch(IOException e1)
{
        System.out.println("ERROR: I/OException "+e1+" occurred in file.");
        S=null;
}

while(S!=null)
{
        MyWindow.WriteLine(S);
         try
        {
                S=InFile.readLine();
        }
        catch(IOException e2)
        {
                System.out.println("ERROR: I/O Exception "+e2+" occurred in file.");
                S=null;
        }
}

try
{
        InFile.close();
}
catch (
{
        System.out.println("ERROR: EXCEPTION: "+e+" occured in trying to close file InFile.");
        System.exit(0);
}

Notice that this is a complete usage of an input file - first open the file by declaring it; then read from it (in this case read all of it); then close it. Note that, inconveniently, we need a new Exception variable in each catch cluase in the class - thus e in the first, e1 in the second, e2 in the third.

You could make the above simpler by enclosing the whole while loop in the try. Thus, we have:

try
{
        InFile=new BufferedReader(new FileReader("file1.txt"));
}
catch(FileNotFoundException e)
{
        System.out.println("ERROR: "+e);
        System.exit(0);
}

try
{
        String S=InFile.readLine();
        while(S!=null)
        {
            MyWindow.WriteLine(S);
             S=InFile.readLine();
        }
        InFile.close();
}
catch(Exception e2)
{
        System.out.println("ERROR: Exception "+e2+" occurred while reading or closing file.");
        S=null;
}

The catch after the try with the while loop would catch any exception in either the readLine statements or the close.

Next

Lynn Ziegler, lziegler@cs.csbsju.edu