15.5 Object Serialization

In Section 15.4, we demonstrated how to write the individual fields of a record into a file as text, and how to read those fields from a file. When the data was output to disk, certain information was lost, such as the type of each value. For instance, if the value "3" is read from a file, there’s no way to tell whether it came from an int, a String or a double. We have only data, not type information, on a disk.

Sometimes we want to read an object from or write an object to a file or over a network connection. Java provides object serialization for this purposes. A serialized object is an object represented as a sequence of bytes that includes the object’s data as well as information about the object’s type and the ...

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.