博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java序列化和反序列化
阅读量:6881 次
发布时间:2019-06-27

本文共 2303 字,大约阅读时间需要 7 分钟。

hot3.png

1、什么是序列化和反序列化

序列化:把对象转换为字节序列的过程。

反序列化:把字节序列恢复成对象的过程。

2、被序列化的类需要实现serializable接口,只是为了标注该对象是可以被序列化的,并没有需要实现的方法。

3、示例:

新建Student类

import java.io.Serializable;public class Student implements Serializable{    private String name;    private int age;    private static final long serialVersionUID = 1L;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }}

新建转换工具类SerializeUtils:

import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;public class SerializeUtils {    public byte[] Serialize(Object object) {        ByteArrayOutputStream byteArrayOutPutStream = new ByteArrayOutputStream();        try {            ObjectOutputStream objectOutPutStream = new ObjectOutputStream(byteArrayOutPutStream);            //将对象写入到字节数组中进行序列化            objectOutPutStream.writeObject(object);            return byteArrayOutPutStream.toByteArray();        }catch (IOException e) {            e.printStackTrace();        }        return null;    }    public Object deSerialize (byte[] bytes) {        //将二进制数组导入字节数据流中        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);        try {            //将字节数组留转化为对象            ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);            return objectInputStream.readObject();        }catch (IOException e) {            e.printStackTrace();        }catch (ClassNotFoundException e) {            e.printStackTrace();        }        return null;    }}

新建测试类test_serializable:

public class test_serializable {    public static void main(String[] args) {        SerializeUtils serializeUtils = new SerializeUtils();        Student student = new Student();        student.setName("kobe");        student.setAge(20);        byte[] serializObject = serializeUtils .Serialize(student);        System.out.println(serializObject);        Student e = (Student)serializeUtils.deSerialize(serializObject);        System.out.println("Name: " + e.getName()+",Age: " + e.getAge());    }}

 

 

 

转载于:https://my.oschina.net/xiaozhiwen/blog/1834751

你可能感兴趣的文章
推荐一个 短链接服务
查看>>
安全管理平台 security management platform
查看>>
CCNA学习笔记之OSPF(理论篇)
查看>>
更改linux主机名
查看>>
centos设置时间自动与网络同步
查看>>
Practice Backbone.js with PHP :: Example 1
查看>>
最新Ghost Win7系统下载(含32位纯净版和64位旗舰版)
查看>>
openssh升级方法
查看>>
46、mysql双主复制实战
查看>>
jQuery(function(){})与(function(){})(jQuery)的区别
查看>>
搞定岳父大人 感
查看>>
EBS OM发运状态 wsh_delivery_details.RELEASED_STATUS
查看>>
JVM内存管理(二)
查看>>
我的友情链接
查看>>
java每日小算法(4)
查看>>
Service Discovery with Marathon, Mesos-DNS and HAProxy
查看>>
【java】TestBufferStream1
查看>>
centos系统,不进行域名解析,resolv.conf配置文件
查看>>
HashMap的长度为什么要是2的n次方
查看>>
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
查看>>