Externalizable接口实现序列化与反序列化

2024-10-09 11:07:43 浏览数 (3)

Externalizable接口实现序列化与反序列化

代码语言:javascript复制
package com.example.core.mydemo.java;

import com.example.core.mydemo.json2.GsonUtils;

import java.io.*;

/**
 * Externalizable接口实现序列化与反序列化
 *
 * Serialization(序列化):将java对象以一连串的字节保存在磁盘文件中的过程,也可以说是保存java对象状态的过程。序列化可以将数据永久保存在磁盘上(通常保存在文件中)。
 * deserialization(反序列化):将保存在磁盘文件中的java字节码重新转换成java对象称为反序列化。
 *
 * Externalizable接口继承Serializable接口,实现Externalizable接口需要实现readExternal()方法和writeExternal()方法,这两个方法是抽象方法,对应的是serializable接口的readObject()方法和writeObject()方法,可以理解为把serializable的两个方法抽象出来
 *
 * output
 * writeExternal>>>
 * Person object has been serialized.
 * readExternal>>>
 * Deserialized Person's name is :{"firstName":"John","lastName":"Doe","sex":"男"}
 *
 */
public class ExternalizableExample {

    public static void main(String[] args) throws IOException, ClassNotFoundException{
        // 创建要被序列化的对象
        Person2 person = new Person2("John", "Doe");
        person.setSex("男");
        // 创建输出流
        FileOutputStream fileOut = new FileOutputStream("person2.ser");
        ObjectOutputStream out = new ObjectOutputStream(fileOut);

        // 将对象写入输出流
        out.writeObject(person);

        // 关闭输出流
        out.close();
        fileOut.close();

        System.out.println("Person object has been serialized.");

        // 从输入流中读取并反序列化对象
        FileInputStream fileIn = new FileInputStream("person2.ser");
        ObjectInputStream in = new ObjectInputStream(fileIn);

        // 从输入流中读取对象
        Person2 deserializedPerson = (Person2)in.readObject();

        // 打印反序列化后的对象信息
        System.out.println("Deserialized Person's name is :"   GsonUtils.toJson(deserializedPerson));

        // 关闭输入流
        in.close();
        fileIn.close();

    }
}


package com.example.core.mydemo.java;

import java.io.*;

public class Person2 implements Externalizable {
    private String firstName;
    private String lastName;
    //transient关键字表示有理的,被修饰的数据不能进行序列化
//    private transient String  sex;
    //static关键字表示静态的,被修饰的数据不能进行序列化
//    private static String  sex;
    private  String  sex;

    public Person2(){
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public Person2(String fname, String lname){
        this.firstName = fname;
        this.lastName = lname;
    }



    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getFirstName(){
        return firstName;
    }

    public String getLastName(){
        return lastName;
    }

    public String getName(){
        return firstName   " "   lastName;
    }

    @Override
    public void writeExternal(ObjectOutput out) throws IOException {
        System.out.println("writeExternal>>>");
        out.writeObject(firstName);
        out.writeObject(lastName);
        out.writeObject(sex);
    }

    @Override
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        System.out.println("readExternal>>>");
        //读写 一一对应,如果按下列顺序来,结果输出如下:Deserialized Person's name is :{"firstName":"男","lastName":"Doe","sex":"John"}
//        sex = (String) in.readObject();
//        lastName = (String) in.readObject();
//        firstName = (String) in.readObject();

        //输出和写入需要保持一致
        firstName = (String) in.readObject();
        lastName = (String) in.readObject();
        sex = (String) in.readObject();
    }
}

0 人点赞