Serialization and deserialization of Java

Source: Internet
Author: User

first explain the two concepts , What is serialization ? What is deserialization ?

Serialization of : The process of transforming an object into a stream is called serialization

deserialization : the process of transforming a stream into an object is called deserialization.

Principles that must be followed for serialization and deserialization

A) Java object

In Java, in order for a Java object to be serializable and deserialized, the class must implement the Java.io.Serializable interface

The Java.io.Serializable interface is defined as follows:

Public Interface Serializable {

}

From the definition above, you can see that there are no methods defined in the interface, which greatly simplifies the developer

b) serialization relies primarily on the Java.io.ObjectOutputStream class, which further encapsulates Java.io.FileOutputStream, where the ObjectOutputStream class is used primarily for writeobject ( ) method to implement serialization functionality

Demo:

/**

* Serializing objects to a disk file

*@paramo

*@throwsException

*/

Public Static void writeobject (Object o) throws exception{

File f=new file ("D:" "user.tmp");

if (F.exists ()) {

F.delete ();

}

FileOutputStream os=New FileOutputStream (f);

ObjectOutputStream Core Class

ObjectOutputStream oos=New objectoutputstream (OS);

Oos.writeobject (o);

Oos.close ();

Os.close ();

}

c) deserialization relies primarily on the Java.io.ObjectInputStream class, which further encapsulates Java.io.InputStream, where the ObjectInputStream class is used primarily for readobject () method to implement serialization functionality

Demo:

/**

* Deserialize, convert disk files to objects

*@paramF

*@return

*@throwsException

*/

Public Static User ReadObject (File f) throws exception{

InputStream is=New FileInputStream (f);

ObjectOutputStream Core Class

ObjectInputStream ois=New ObjectInputStream (IS);

return (User) Ois.readobject ();

}

Post a complete demo

Java objects:

Package Com.io.bean;

import java.io.Serializable;

Public class User implements Serializable {

Private intuserId;

Private String UserName;

Private String Usersex;

Private intuserage;

Public int getuserage () {

returnuserage;

}

Public void setuserage (int userage) {

this. userage = Userage;

}

Public int getUserId () {

returnuserId;

}

Public void setuserid (int userId) {

this. userid = userid;

}

Public String GetUserName () {

returnUserName;

}

Public void setusername (String userName) {

this. userName = UserName;

}

Public String Getusersex () {

returnUsersex;

}

Public void setusersex (String usersex) {

this. usersex = Usersex;

}

@Override

Public String toString () {

return this. GetUserId () + "" + this. GetUserName () + ""

+ this. Getusersex () + "" + this. Getuserage ();

}

}

Serialization and deserialization

Package com.io.test;

import java.io.File;

import Java.io.FileInputStream;

import Java.io.FileOutputStream;

import Java.io.InputStream;

import Java.io.ObjectInputStream;

import Java.io.ObjectOutputStream;

import Com.io.bean.User;

Public class testserializable {

/**

* Serializing objects to a disk file

*@paramo

*@throwsException

*/

Public Static void writeobject (Object o) throws exception{

File f=new file ("D:" "user.tmp");

if (F.exists ()) {

F.delete ();

}

FileOutputStream os=New FileOutputStream (f);

ObjectOutputStream Core Class

ObjectOutputStream oos=New objectoutputstream (OS);

Oos.writeobject (o);

Oos.close ();

Os.close ();

}

/**

* Deserialize, convert disk files to objects

*@paramF

*@return

*@throwsException

*/

Public Static User ReadObject (File f) throws exception{

InputStream is=New FileInputStream (f);

ObjectOutputStream Core Class

ObjectInputStream ois=New ObjectInputStream (IS);

return (User) Ois.readobject ();

}

Public Static void Main (string[] args) throws exception{

/***************** serializing an Object ***************/

/*

User User=new user ();

User.setuserid (1);

User.setusername ("Zhang Yimou");

User.setusersex ("male");

User.setuserage (50);

Testserializable.writeobject (user);

*/

/***************** the object sequence back ***************/

User user=testserializable. ReadObject (new File ("D:" "user.tmp"));

System. out. println (user);

}

}

Serialization and deserialization of Java

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.