public class BinaryInputDemo2
1: //BinaryInputDemo2.java
2:
3: import java.io.FileInputStream;
4: import java.io.ObjectInputStream;
5: import java.io.EOFException;
6: import java.io.FileNotFoundException;
7: import java.io.IOException;
8: import java.util.Scanner;
9:
10: public class BinaryInputDemo2
11: {
12: public static void main(String[] args)
13: {
14: System.out.print("Enter file name: ");
15: Scanner keyboard = new Scanner(System.in);
16: String fileName = keyboard.next();
17:
18: try
19: {
20: ObjectInputStream inputStream =
21: new ObjectInputStream(new FileInputStream(fileName));
22: System.out.println("Reading the nonnegative integers");
23: System.out.println("in the file " + fileName);
24: int anInteger = inputStream.readInt();
25: while (anInteger >= 0)
26: {
27: System.out.println(anInteger);
28: anInteger = inputStream.readInt();
29: }
30: System.out.println("End of reading from file.");
31: inputStream.close();
32: }
33: catch (FileNotFoundException e)
34: {
35: System.out.println("Problem opening the file " + fileName);
36: }
37: catch (EOFException e)
38: {
39: System.out.println("Problem reading the file " + fileName);
40: System.out.println("Reached end of the file.");
41: }
42: catch (IOException e)
43: {
44: System.out.println("Problem reading the file " + fileName);
45: }
46: }
47: }