Whether it is a word-processing application that saves documents to a disk, a utility that remembers its configuration for subsequent use, or a game that allows players to assume world domination and then set it aside for the night, the ability to store and later retrieve data is vital. Without it, software would be hardly more effective than the typewriterusers would have to retype their data to make further modifications once their applications exit.
Most Java programmers are comfortable writing and reading basic Java data types like strings and integers to and from disk. However, writing code that saves abstract data types, like any object you have written yourself, can become both complex and repetitive. First, you have to create a specification for the proposed file structure. Next, you have to implement save and restore functions that convert the object data to and from primitive data types, and finally you need to test it with sample data.
If you need to store new data to the application later, you'll have to modify the file specification, as well as the save and restore methods. It would be nice if you could simply save an abstract data typelike a linked list, for examplejust as easily as you save, say, an integer data value, without having to iterate through all its nodes, saving each one in turn.
Happily, Java does provide a solution that does just that, object serialization. Object serialization takes an object's state and converts it to a stream of data. With object serialization, making any object persistent becomes easyyou dont have to write custom code to save object member variables. The object can be restored at a later time, even at another location. In fact, you can even move an object from one computer to another and have it maintain its state. Not only is this a very cool feature of Java, but it also is easy to use.