Java Reference
In-Depth Information
Display 10.17
Using
EOFException
(part 1 of 2)
1
import
java.io.ObjectInputStream;
2
import
java.io.FileInputStream;
3
import
java.io.EOFException;
4
import
java.io.IOException;
5
import
java.io.FileNotFoundException;
6
public
class
EOFDemo
7{
8
public
static
void
main(String[] args)
9
{
10
try
11
{
12
ObjectInputStream inputStream =
13
new
ObjectInputStream(
new
FileInputStream("numbers.dat"));
14
int
number;
15
System.out.println("Reading numbers in numbers.dat");
16
try
17
{
18
while
(
true
)
19
{
20
number = inputStream.readInt();
21
System.out.println(number);
22
}
23
}
24
catch
(EOFException e)
25
{
26
System.out.println("No more numbers in the file.");
27
}
28
inputStream.close();
29
}
30
catch
(FileNotFoundException e)
31
{
32
System.out.println("Cannot find file numbers.dat.");
33
}
34
catch
(IOException e)
35
{
36
System.out.println("Problem with input from file numbers.dat.");
37
}
38
}
39
}
(continued)