[ Pobierz całość w formacie PDF ]

of a file in the wrong way, one of two things will probably happen: Your program will either go
into an unintended infinite loop or terminate abnormally.
5640_ch10.fm Page 555 Wednesday, February 11, 2004 2:33 PM
Binary Files 555
Display 10.13 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 }
5640_ch10.fm Page 556 Wednesday, February 11, 2004 2:33 PM
556 Chapter 10 File I/O
Display 10.13 Using EOFException (Part 2 of 2)
SAMPLE DIALOGUE
Reading numbers in numbers.dat
Assumes the program in Display 10.9
1
was run to create the file
2
numbers.dat.
3
4
No more numbers in the file.
For the classes discussed in this book, the following rules apply: If your program is reading from a
binary file, then an EOFException will be thrown when the reading goes beyond the end of the
file. If your program is reading from a text file, then some special value, such as null, will be
returned when your program attempts to read beyond the end of the file, and no EOFException
will be thrown.
Self-Test Exercises
35. When opening a binary file for output in the ways discussed in this chapter, might an
exception be thrown? What kind of exception? When opening a binary file for input in the
ways discussed in this chapter, might an exception be thrown? What kind of exception?
36. Suppose a binary file contains three numbers written to the file with the method write-
Double of the class ObjectOutputStream. Suppose further that your program reads all
three numbers with three invocations of the method readDouble of the class ObjectIn-
putStream. When will an EOFException be thrown? Right after reading the third num-
ber? When your program tries to read a fourth number? Some other time?
37. The following appears in the program in Display 10.13:
try
{
while (true)
{
number = inputStream.readInt();
System.out.println(number);
}
}
catch(EOFException e)
{
5640_ch10.fm Page 557 Tuesday, February 17, 2004 5:34 PM
Binary Files 557
System.out.println("No more numbers in the file.");
}
Why isn t this an infinite loop?
BINARY I/O OF OBJECTS
You can output objects of classes you define as easily as you output int values using
writeInt, and you can later read the objects back into your program as easily as you read
int values with the method readInt. For you to be able to do this, the class of objects
that your code is writing and reading must implement the Serializable interface.
We will discuss interfaces in general in Chapter 13. However, the Serializable
interface is particularly easy to use and requires no knowledge of interfaces. All you Serializable
interface
need to do to make a class implement the Serializable interface is add the two words
implements Serializable to the heading of the class definition, as in the following
example:
public class Person implements Serializable
{
The Serializable interface is in the same java.io package that contains all the I/O
classes we have discussed in this chapter. For example, in Display 10.14 we define a toy
class named SomeClass that implements the Serializable interface. We will explain the
effect of the Serializable interface a bit later in this chapter, but first let s see how you
do binary file I/O with a serializable class such as this class SomeClass in Display 10.14.
Display 10.15 illustrates how class objects can be written to and read from a binary
file. To write an object of a class such as SomeClass to a binary file, you simply use the
method writeObject of the class ObjectOutputStream. You use writeObject in the writeObject
same way that you use the other methods of the class ObjectOutputStream, such as
writeInt, but you use an object as the argument.
If an object is written to a file with writeObject, then it can be read back out of the
file with readObject of the stream class ObjectInputStream, as also illustrated in Dis- readObject
play 10.14. The method readObject returns its value as an object of type Object. If
you want to use the values returned by readObject as an object of a class like Some-
Class, you must do a type cast, as shown in Display 10.15.
THE Serializable INTERFACE
A class that implements the Serializable interface is said to be a serializable class. To serializable [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • sportingbet.opx.pl