Home Segments Top Top Previous Next

639: Sidetrip

You can, in principle, write more than one class instance into a file, but then, to read such a file, your program must know how many class instances have been written, because there is no direct way to test an ObjectInputStream instance to determine whether all instances have been read.

Thus, you could use writeObject to store a collection of Movie instances; however, to read those instances using readObject, your program either would have to know how many movies have been written or else make use of the exception throwing done by readObject when it tries to read past the end of a file.

Evidently, the designers of Java believe that, if you have a collection of instances to store, you should store it as a collection, rather than store each element of the collection individually.