Posted by : Rhyf Ahmad Saturday, May 23, 2015

In this chapter, you see how you can transfer objects to and from a stream. This greatly simplifi es file I/O in your object-oriented programs. In most circumstances, the details of writing all the information that makes up an object is taken care of completely and automatically. Similarly, objects are typically reconstructed automatically from what you wrote to the file.
The process of storing and retrieving objects in an external file is called serialization. Writing an object to a file is referred to as serializing, and reading an object from a file is called deserializing. Serialization is concerned with writing objects and the fields they contain to a stream, so static member data is not included. Static fi elds have whatever values are assigned by default in the class defi nition. Note that an array of any type is an object and can be serialized — even an array of values of a primitive type, such as type int or type double.
I think you might be surprised at how easy this is. Perhaps the most impressive aspect of the way serialization is implemented in Java is that you can generally read and write objects of almost any class type, including objects of classes that you have defi ned yourself, without adding any code to the classes involved to support this mechanism. For the most part, everything is taken care of automatically.
Two classes from the java.io package are used for serialization. An ObjectOutputStream object manages the writing of objects to a fi le, and reading the objects back is handled by an object of the class ObjectInputStream. As Figure 12-1 illustrates, these classes are derived from OutputStream and InputStream, respectively.

Contents:

Storing Objects in a File
Writing an Object to a File

  1. Writing Primitive Data Types to an Object Stream
  2. Implementing the Serializable Interface
Reading an Object from a File

  1. Determining the Class of a Deserialized Object
  2. Using Object Serialization
  3. Serializing Classes Yourself
  4. Serialization Problems and Complications 



Ebook Preview
Get the ebook now (371 KB)

If the link above doesn't work, try the link below


Mirror: Get the ebook now (371 KB)

Leave a Reply

You can leave a comment to request any article(s), software(s), ebook(s) or report the dead link.
Then I will reply as soon as I can :)

Subscribe to Posts | Subscribe to Comments

Welcome to My Blog

Recent Post

Recent Posts Widget
Instagram

- Copyright © Madriva -Robotic Notes- Powered by Blogger - Designed by Johanes Djogan -