//EOFExceptionDemo.java

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

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