15.5.2 Reading and Deserializing Data from a Sequential-Access File

The preceding section showed how to create a file for sequential access using object serialization. In this section, we discuss how to read serialized data sequentially from a file.

The program in Fig. 15.11 reads records from a file created by the program in Section 15.5.1 and displays the contents. The program opens the file for input by calling Files static method newInputStream, which receives a Path specifying the file to open and, if the file exists, returns an InputStream that can be used to read from the file. In Fig. 15.10, we wrote objects to the file, using an ObjectOutputStream object. Data must be read from the file in the same format in which it was written. Therefore, ...

Get Java™ How To Program (Early Objects), Tenth 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.