//EOFExceptionDemo2.java
//Trying to avoid the nested try blocks, by putting
//another try block in a catch block.

import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.io.FileNotFoundException;
import java.io.EOFException;
import java.io.IOException;

public class EOFExceptionDemo2
{
    public static void main(String[] args)
    {
        String fileName = "numbers.dat";
        ObjectInputStream inputStream = null;
        try
        {
            inputStream = new ObjectInputStream(new FileInputStream(fileName));
            System.out.println("\nReading all integers from the file "
                + fileName + ":");
            while (true)
            {
                int anInteger = inputStream.readInt();
                System.out.println(anInteger);
            }
        }
        catch(FileNotFoundException e)
        {
            System.out.println("Error opening the file " + fileName + ".");
        }
        catch(EOFException e)
        {
            System.out.println("Finished reading from the file.");
            try
            {
                inputStream.close();
            }
            catch(IOException ee)
            {
            }
        }
        catch(IOException e)
        {
            System.out.println("Error reading input from file " + fileName + ".");
        }
    }
}
