Serialization of Java-implemented objects

Source: Internet
Author: User

What is the serialization of an object?

Serialization: The process of converting an object to a sequence of bytes is the serialization of an object; deserialization: The process of converting a sequence of bytes into an object is the deserialization of an object. Just look at the concept of abstraction, but look at the code will understand.

Purpose of Object serialization

1, Java program in the run, the object is allocated in memory, and one of the purposes of serialization is to convert the object in memory into the object on disk.

2, objects can not be transmitted directly in the network, sometimes need to convert the object into a sequence of bytes in the network transmission, which is the second purpose of object serialization.

How can I implement serialization of an object?

1, create a new entity object, implement serializable interface, this entity object can be serialized

 Packagetest.entity;Importjava.io.Serializable;/*** Entity class, for serialization * *@authorYaoqi * @date June 24, 2017*/ Public classPersonImplementsSerializable {/*** Serialization ID*/    Private Static Final LongSerialversionuid = -3245478690496182643l; PrivateString name; Private intAge ;  PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; } @Override PublicString toString () {return"Person [name=" + name + ", age=" + Age + "]"; }}

2, the entity class is serialized in another class, and the serialized sequence of bytes exists on the disk.

 Packagetest.serializable;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;ImportJava.io.ObjectInputStream;ImportJava.io.ObjectOutputStream;Importjava.util.Date;ImportTest.entity.Person;/*** Serialization of Java-implemented objects * *@authorYaoqi * @date June 24, 2017*/ Public classSerializabledemo { Public Static voidMain (string[] args)throwsException {//serialization of an objectObjectOutputStream Objoutputstream =NewObjectOutputStream (NewFileOutputStream ("D:\\obj.txt")); Person Person=NewPerson (); Person.setname ("Tom"); Person.setage (24); //write literal literals constantObjoutputstream.writeobject ("Serializing an object"); //Writing anonymous ObjectsObjoutputstream.writeobject (NewDate ()); //writing to an objectObjoutputstream.writeobject (person); //Deserialize an objectObjectInputStream Objinputstream =NewObjectInputStream (NewFileInputStream ("D:\\obj.txt")); System.out.println ("Read literal constants:" +(String) objinputstream.readobject ()); System.out.println ("Read Anonymous object" +(Date) objinputstream.readobject ()); System.out.println ("Read Person object" +(person) Objinputstream.readobject ()) . toString ()); }}

After execution, the console results are as follows:

Read literal constants: Serializing an Object
Anonymous objects read Sat June 22:42:22 CST 2017
Read Person Object person [Name=tom, age=24]

Serialization of Java-implemented objects

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.