10.5 Binary-File I/O with Objects and Arrays

In this section, we cover I/O for binary files involving objects of a class and arrays, which, you’ll recall, are really objects. We will use the classes ObjectInputStream and ObjectOutputStream.

Binary-File I/O with Objects of a Class

We have seen how to write primitive values and strings to a binary file, and how to read them again. How would we write and read objects other than strings? We could, of course, write an object’s instance variables to a file and invent some way to reconstruct the object when we read the file. Since an instance variable could be another object that itself could have an object as an instance variable, however, completing this task sounds formidable.

Fortunately, Java provides ...

Get Java: An Introduction to Problem Solving and Programming, 8th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.