Java object serialization principle and Serializable interface

Source: Internet
Author: User

The serialization of a Java object is one of the methods of turning an object into a binary data stream, which can be easily transferred and stored by the serialization of the object.

The principle is: Object-------------> Transform-----------------> Transform an object into binary data

When an object is serialized, only its properties are serialized (each object has the same method, but the properties of each object are not necessarily the same, i.e. the object holds only its property information)

Then the object of a class to be serialized, this class must implement the Java.io.Serializable interface, the source code is as follows:

Public interface Serializable {}

You can see that this interface does not have any methods defined, this interface is just an identity interface that indicates that a class has the ability to serialize.

If you want to complete an object's input or output, you must rely on the input stream (ObjectInputStream) of the object and the object output stream (ObjectOutputStream).

1. The process of outputting a serialized object using an object output stream is called serialization.

2. The process of using an object input stream to read into an object is called a deserialization session.

Program-------------->objectoutputstream-------------------> Serialized Objects <---------------------- ObjectInputStream-----------------------------Program

Serialization deserialization

One, ObjectOutputStream object output stream

He is a subclass of OutputStream bytes out of stream, mainly in the following ways:

1. Public ObjectOutputStream (OutputStream out) throw IOException construction method such as incoming output stream object

2. Public final void WriteObject (object obj) throw IOException Output Object

The implementation is as follows:

Defines a serialization class.

Package Andy.serializable.test;import java.io.serializable;/** * @author zhang,tianyou version:2014-11-20 2:41:12 *< c0/>*  */public class Student implements Serializable {/** *  */private static final long Serialversionuid = 6095384 564471868081l;//implementation serializable refers to becoming a serialized class private String name;private int age;public Student () {}public Student (String Name, int age) {this.name = Name;this.age = age;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;}}

To output an object to a file in the D:tes.txt file:

Package Andy.serializable.test;import Java.io.file;import Java.io.fileoutputstream;import java.io.IOException; Import Java.io.objectoutputstream;import java.io.outputstream;/**   * @author zhang,tianyou   * version:2014-11-20 pm 2:45:24   *  *   */public class Objectoutputstreamtest {/** * @param args * @throws  IOException  */public static void Main (string[] args) throws IOException {//TODO auto-generated method Stubfile file = New File ("D:" + File.separator + "test.txt"); ObjectOutputStream oos = Null;outputstream outputstream = new Fileoutputstre AM (file), Oos = new ObjectOutputStream (outputstream), Oos.writeobject (New Student ("Xiaoming"), Outputstream.close (); O Os.close ();}}

The results of the binary data are as follows:



Second, ObjectInputStream object input stream

The ability to deserialize a serialized object. is a subclass of InputStream, instantiation is a inputstream input stream object that must be accepted.

The main methods are as follows:

1. ObjectInputStream (InputStream in) throw IOException construction method

2, Public final object ReadObject () throw IOException, ClassNotFoundException reads the object at the specified position

The implementation is as follows:

Package Andy.serializable.test;import Java.io.file;import Java.io.fileinputstream;import java.io.IOException; Import Java.io.inputstream;import java.io.objectinputstream;/** * @author zhang,tianyou version:2014-11-20 2:45:46 *  *  */public class Objectinputstreamtest {/** * @param args * @throws IOException * @throws classnotfoundexception */public static void Main (string[] args) throws Ioexception,classnotfoundexception {//TODO auto-generated method Stubfil E file = new file ("D:" + File.separator + "test.txt"); ObjectInputStream ois = Null;inputstream InputStream = new FileInput Stream (file); ois = new ObjectInputStream (inputstream); Object obj = Ois.readobject (); Student Student = (Student) obj;ois.close (); Inputstream.close (); SYSTEM.OUT.PRINTLN ("Test content: Name:" + student.getname () + "Age:" + student.getage ());}}

Run as follows:

Test content: Name Xiaoming age:23


Third, you can also customize the serialization interface, you can specify what you want to serialize, just implement the Externalizable interface.


The operation of the serializable interface is actually to serialize all the properties of an object, but it is also possible to serialize the partial attributes through the Externalizable interface, but it is easier to declare an interface that does not require serialization using the transient keyword.

For example:

private transient String name;

Then name is not serialized.


Java object serialization principle and Serializable interface

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.