public class EOFExceptionDemo2
1: //EOFExceptionDemo2.java
2: //Trying to avoid the nested try blocks, by putting
3: //another try block in a catch block.
5: import java.io.FileInputStream;
6: import java.io.ObjectInputStream;
7: import java.io.FileNotFoundException;
8: import java.io.EOFException;
9: import java.io.IOException;
11: public class EOFExceptionDemo2
12: {
13: public static void main(String[] args)
14: {
15: String fileName = "numbers.dat";
16: ObjectInputStream inputStream = null;
17: try
18: {
19: inputStream = new ObjectInputStream(new FileInputStream(fileName));
20: System.out.println("\nReading all integers from the file "
21: + fileName + ":");
22: while (true)
23: {
24: int anInteger = inputStream.readInt();
25: System.out.println(anInteger);
26: }
27: }
28: catch(FileNotFoundException e)
29: {
30: System.out.println("Error opening the file " + fileName + ".");
31: }
32: catch(EOFException e)
33: {
34: System.out.println("Finished reading from the file.");
35: try
36: {
37: inputStream.close();
38: }
39: catch(IOException ee)
40: {
41: }
42: }
43: catch(IOException e)
44: {
45: System.out.println("Error reading input from file " + fileName + ".");
46: }
47: }
48: }