Java序列化

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Java序列化

Black·Tea   2022-05-23 我要评论

以前用序列化都是一些方法需要才实现的,后来业务需求要深拷贝才去研究。参阅了别人博客得出一些总结。

序列化是为了把Java对象转化为字节序列(字节流)的过程。然后深拷贝是通过对流的操作来实现的,序列化后数据方便存储传输。反序列化则是把字节序列反序列化为Java对象

存储方便:因为对象会被回收,序列化后可以持续化存储在磁盘中
传输方便:字节序列(二进制形式)可以进行网络传输和传播。

最好设置一个SerialversionUID,因为序列化和反序列化是对比SerialversionUID来进行的,虽然不设置接口也会默认生成一个,但是要知道序列化对象过程一般都是对象->序列化->存储或传输->反序列化。
举个例子:
先创建一个实体类Student

import lombok.Data;

import java.io.Serializable;

@Data
public class Student implements Serializable {
    private Integer id;
    private String name;
    private String sex;
}
然后创建一个测试类SerializableTest
import serialization.entity.Student;

import java.io.*;

public class SerializableTest {
    public static void main(String[] args) throws Exception {
        serializeStudent();
        Student student = deserializeStudent();
        System.out.println("name:" + student.getName());
        System.out.println("sex:" + student.getSex());
    }

    private static void serializeStudent() throws IOException {
        Student student = new Student();
        student.setId(1);
        student.setName("张三");
        student.setSex("male");

        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(
                new File("F:/student.txt")));
        out.writeObject(student);
        System.out.println("序列化成功");
        out.close();
    }

    private static Student deserializeStudent() throws Exception {
        ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File("F:/student.txt")));
        Student student = (Student) in.readObject();
        System.out.println("反序列化成功");
        return student;
    }
}
执行结果:
序列化成功
反序列化成功
name:张三
sex:male
这个时候没有指定SerialversionUID也是可以成功的,但对象->序列化->存储或传输->反序列化,咱们在反序列化操作之前对Student类修改呢?

这个时候咱们修改一下代码,先注释掉反序列化代码,先进行序列化。

import serialization.entity.Student;

import java.io.*;

public class SerializableTest {
    public static void main(String[] args) throws Exception {
        serializeStudent();
//        Student student = deserializeStudent();
//        System.out.println("name:" + student.getName());
//        System.out.println("sex:" + student.getSex());
    }

    private static void serializeStudent() throws IOException {
        Student student = new Student();
        student.setId(1);
        student.setName("张三");
        student.setSex("male");

        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(
                new File("F:/student.txt")));
        out.writeObject(student);
        System.out.println("序列化成功");
        out.close();
    }

//    private static Student deserializeStudent() throws Exception {
//        ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File("F:/student.txt")));
//        Student student = (Student) in.readObject();
//        System.out.println("反序列化成功");
//        return student;
//    }
}
运行结果:
序列化成功

修改Student类

import lombok.Data;

import java.io.Serializable;

@Data
public class Student implements Serializable {
    private Integer id;
    private String name;
    private String sex;
    private String address;
}
注释掉序列化方法,进行反序列化
import serialization.entity.Student;

import java.io.*;

public class SerializableTest {
    public static void main(String[] args) throws Exception {
//        serializeStudent();
        Student student = deserializeStudent();
        System.out.println("name:" + student.getName());
        System.out.println("sex:" + student.getSex());
    }

//    private static void serializeStudent() throws IOException {
//        Student student = new Student();
//        student.setId(1);
//        student.setName("张三");
//        student.setSex("male");
//
//        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(
//                new File("F:/student.txt")));
//        out.writeObject(student);
//        System.out.println("序列化成功");
//        out.close();
//    }

    private static Student deserializeStudent() throws Exception {
        ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File("F:/student.txt")));
        Student student = (Student) in.readObject();
        System.out.println("反序列化成功");
        return student;
    }
}
执行结果:
Exception in thread "main" java.io.InvalidClassException: serialization.entity.Student; local class incompatible: stream classdesc serialVersionUID = 3846952599709361171, local class serialVersionUID = -4606152942663467236
	at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:699)
	at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1885)
	at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1751)
	at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2042)
	at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1573)
	at java.io.ObjectInputStream.readObject(ObjectInputStream.java:431)
	at serialization.demo.SerializableTest.deserializeStudent(SerializableTest.java:30)
	at serialization.demo.SerializableTest.main(SerializableTest.java:10)

Process finished with exit code 1
可以看出两次的执行的SerialversionUID不匹配,导致产生java.io.InvalidClassException异常,所以只要指定了SerialversionUID就不会报异常。
//指定serialVersionUID正确写法
private static final long serialVersionUID = 3846952599709361171L;

//如果已经进行序列化了不知道SerialversionUID,可以通过反射获取
Object obj = Student.class.newInstance();
Field field = Student.class.getDeclaredField("serialVersionUID");
field.setAccessible(true);
System.out.println(field.getLong(obj));

最后需要知道的一点就是字节流字符流的区别。
字节流:传输过程中,传输数据的最基本单位是字节的流。
字符流:传输过程中,传输数据的最基本单位是字符的流。

这样讲可能有点不知所云,字节其实就是Java的八大基本类型Byte(比特)单位,而字符通常是’A’、‘B’、’$’、’&'等,字节大小则取决于你是什么编码(环境),如下:

ASCII 码中,一个英文字母(不分大小写)为一个字节,一个中文汉字为两个字节。

UTF-8 编码中,一个英文字为一个字节,一个中文为三个字节。

Unicode 编码中,一个英文为一个字节,一个中文为两个字节。

符号:英文标点为一个字节,中文标点为两个字节。例如:英文句号 . 占1个字节的大小,中文句号 。

占2个字节的大小。UTF-16 编码中,一个英文字母字符或一个汉字字符存储都需要 2 个字节(Unicode 扩展区的一些汉字存储需要 4 个字节)。

UTF-32 编码中,世界上任何字符的存储都需要 4 个字节。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们