首页 > 开发 > Java > 正文

详解Java 序列化与反序列化(Serialization)

2019-10-21 18:22:45
字体:
来源:转载
供稿:网友

一、什么是?为什么需要?

序列化(Serialization)是将对象的状态信息转化为可以存储或者传输的形式的过程,反序列化则为其逆过程。

内存的易失性;传输需要;一些应用场景中需要将对象持久化下来,以便在需要的时候进行读取。

二、JDK提供的API

java.io.ObjectOutputStream类的 writeObject(Object obj)方法

java.io.ObjectInputStream类的readObject()方法

对于Serializable,如果没有重写 writeObject和readObject,则调用默认的方法

Externalizable继承了Serializable,多了2个方法:writeExternal和readExternal,用来控制需要序列化哪些字段

三、实现方法

假定一个Person类,实现了Serializable或Externalizable接口

import java.io.Serializable;/** * @Author: pf_xu * @Date: 2019/3/5 12:37 * @Version 1.0 */public class Person implements Serializable { private int age; private String name; public Person(int age, String name) { this.age = age; this.name = name; } public void setAge(int age) { this.age = age; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public String getName() { return name; }}
import java.io.Externalizable;import java.io.IOException;import java.io.ObjectInput;import java.io.ObjectOutput;/** * @Author: pf_xu * @Date: 2019/3/5 13:01 * @Version 1.0 */public class SpecialPerson implements Externalizable { private int age; private String name; public SpecialPerson(){} public SpecialPerson(int age, String name) { this.age = age; this.name = name; } public void setAge(int age) { this.age = age; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public String getName() { return name; } @Override public void writeExternal(ObjectOutput out) throws IOException { out.writeObject(age); out.writeObject(name); } @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { this.age = (Integer) in.readObject(); this.name = (String)in.readObject(); }}
import java.io.*;/** * @Author: pf_xu * @Date: 2019/3/5 12:40 * @Version 1.0 */public class SerializableDemo { public static void main(String[] args) throws IOException, ClassNotFoundException { Person person = new Person(10,"Simon"); ObjectOutputStream oos1 = new ObjectOutputStream(new FileOutputStream("object1.out")); oos1.writeObject(person); ObjectInputStream ois1= new ObjectInputStream(new FileInputStream("object1.out")); Person re_person = (Person) ois1.readObject(); System.out.println(re_person.getName()+"---"+re_person.getAge()); SpecialPerson specialPerson = new SpecialPerson(30,"Daniel"); ObjectOutputStream oos2 = new ObjectOutputStream(new FileOutputStream("object2.out")); oos2.writeObject(specialPerson); ObjectInputStream ois2= new ObjectInputStream(new FileInputStream("object2.out")); SpecialPerson re_specialPerson = (SpecialPerson)ois2.readObject(); System.out.println(re_specialPerson.getName()+"---"+re_specialPerson.getAge()); }}

  四、一些细节

1.序列化ID

serialVersionUID  如果两个类的ID不同,则不能互相序列与反序列(可应用与版本控制,不同版本的类相互兼容或者不兼容)

2.安全性

由于其标准化导致其有泄露的风险(二进制明文,可采用加密的方法)

以上所述是小编给大家介绍的Java序列化和反序列化详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对CuoXin错新网网站的支持!


注:相关教程知识阅读请移步到JAVA教程频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表