Browse DevX
Sign up for e-mail newsletters from DevX


Implement Persistent Objects with Java Serialization

One of the most critical operations an application can perform is saving and restoring its state between successive runs. Yet, saving data in a textual format is not always practical--or secure. Using serialization, you can save any Java object to disk in a flash.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

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 typewriter—users 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 type—like a linked list, for example—just 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 easy—you 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.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date