Java IO5: Serialization and deserialization

Source: Internet
Author: User

I. The concept of serialization and deserialization
The process of converting an object to a sequence of bytes is called serialization of an object.
The process of reverting a sequence of bytes to an object is called deserialization of the object.
There are two main uses for serialization of objects:
1) The object's byte sequence is permanently saved to the hard disk, usually stored in a file;
2) A sequence of bytes that transmits an object over the network.
In many applications, some objects need to be serialized to leave the memory space and stay on the physical hard disk for long-term storage. For example, the most common is the Web server session object, when there are 100,000 users concurrent access, there may be 100,000 session objects, memory may be unbearable, so the Web container will be some seesion first serialized to the hard disk, and so on, Restore the objects that were saved to the hard disk in memory.
When two processes are communicating remotely, each other can send various types of data. Regardless of the type of data, it is transmitted over the network in the form of a binary sequence. The sender needs to convert the Java object to a sequence of bytes to be transmitted over the network, and the receiver needs to revert the byte sequence back to the Java object.

An object must implement the Serializable interface or the Externalizable interface to be able to serialize. , there is no method defined in the interface, it is an identifying interface (Marker Interface), and when a class implements that interface, it means that the object of the class can be serialized.

• When an object is serialized, only non-static member variables of the object are saved, and no member methods and static member variables can be saved.
• If the member variable of an object is an object, the data members of the object are also saved.
• If a serializable object contains a reference to an object that is not serializable, the entire serialization operation will fail and a notserializableexception will be thrown. We can mark this reference as transient, so the object can still be serialized

The static variable is not serializable at serialization, and if a contains a reference to B, then B is serialized as well when serializing a, and if A is serializable and B cannot be serialized, then an exception occurs when serializing a, and the reference to B is set to T, then Ransient, the keyword indicates that the variable is not serialized.

Second, the serialization API in the JDK class library
Java.io.ObjectOutputStream represents an object output stream, and its writeobject (object obj) method serializes the Obj object specified by the parameter and writes the resulting sequence of bytes to a target output stream.
Java.io.ObjectInputStream represents an object input stream, and its readobject () method reads a sequence of bytes from a source input stream, deserializes them into an object, and returns them.
Only objects of classes that implement the serializable and Externalizable interfaces can be serialized. The Externalizable interface inherits from the serializable interface, and the class that implements the Externalizable interface controls the serialization behavior entirely by itself, whereas classes that implement the serializable interface can take the default serialization method.
Object serialization consists of the following steps:
1) Create an object output stream, which can wrap a different type of target output stream, such as a file output stream;
2) writes an object through the WriteObject () method of the object output stream.
The steps for deserializing an object are as follows:
1) Create an object input stream, which can wrap a different type of source input stream, such as a file input stream;
2) The object is read through the ReadObject () method of the object input stream.

Examples of object serialization and deserialization:

 Public classPersonImplementsSerializable {PrivateString name; Private intAge ; Private Doubleheight;  Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; }     Public Doublegetheight () {returnheight; }     Public voidSetHeight (Doubleheight) {         This. Height =height; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }}

Serializing and deserializing a person class object

 Public classTestobjserializeanddeserialize { Public Static voidMain (string[] args)throwsException {//serializing A Person objectSerializeperson (); //deserialize a person objectPerson person =Deserializeperson (); System.out.println (Person.getname ()+ "," + person.getage () + "," +person.getheight ()); }    Private Static voidSerializeperson ()throwsException {
Person Person1
=NewPerson (); Person1.setname ("Zhangsan"); Person1.setage (20); Person1.setheight (170.1); //The ObjectOutputStream object output stream, which stores the person object in the Person.txt file of the C drive,//to complete a serialization operation on a person objectObjectOutputStream Oos =NewObjectOutputStream (NewFileOutputStream (NewFile ("C:\\person.txt"))); Oos.writeobject (Person1); Oos.close (); } Private StaticPerson Deserializeperson ()throwsException {objectinputstream ois=NewObjectInputStream (NewFileInputStream (NewFile ("C:\\person.txt"))); Person Person=(person) ois.readobject (); returnPerson ; }}

The serialized person succeeds in generating a Person.txt file on the C drive, while deserializing the person is reading the e-disk Person.txt a Person object

Java IO5: Serialization and deserialization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.