java之序列化与反序列化

  • 2021年10月29日
  • Java

序列化与反序列化要点

1) 只要实现java.io.Serializable接口,那么它就可以被序列化,并且要求序列化的类必须属于Enum、Array和Serializable类型其中的任何一种。

2) 通过ObjectOutputStream和ObjectInputStream对象进行序列化及反序列化

3) 要想将父类对象也序列化,就需要让父类也实现Serializable 接口

4) 虚拟机是否允许反序列化,不仅取决于类路径和功能代码是否一致, 一个非常重要的一点是两个类的序列化 ID 是否一致(就是 private static final long serialVersionUID)

5) 可以通过添加transient修饰关键字,阻止该变量被序列化到文件中

6) 在要序列化的类中添加writeObject 和 readObject方法可以实现自定义序列化

一、测试对象 user1.java

/**
 * @项目名称 longlonggo-sample
 * @文件名称 User1.java  版本号:1.0
 * @创建日期 2018年9月30日 
 * @创建作者 me@longlonggo.com
 */
package serialization;

import java.io.Serializable;

/**
 * 说明:
 * 
 * @version 1.0
 * @author me@longlonggo.com
 *
 */
public class User1 implements Serializable {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" + "name='" + name + ''' + ", age=" + age + '}';
    }
}

二、测试主方法 SerializableDemo1.java

/**
 * @项目名称 longlonggo-sample
 * @文件名称 SerializableDemo1.java  版本号:1.0
 * @创建日期 2018年9月30日 
 * @创建作者 me@longlonggo.com
 */
package serialization;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

/**
 * 说明:序列化测试     
 * @version 1.0
 * @author me@longlonggo.com
 *
 */
public class SerializableDemo1 {
    public static void main(String[] args) throws IOException {
        User1 user= new User1();
        user.setAge(26);
        user.setName("longlonggo");
        System.out.println(user);
        
        String fileUrl = "d://tmpfile.txt";
        
        //将对象序列化输出到文件
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream(fileUrl));
            oos.writeObject(user);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //注意该处,写入完毕后,必须进行关闭操作,不然会在下面出现删除占用异常
            IOUtils.closeQuietly(oos);
        }
        
        //从文件中读取序列化对象,然后反序列化为java对象
        File file = new File(fileUrl);
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(new FileInputStream(file));
            User1 newUser = (User1) ois.readObject();
            System.out.println(newUser);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            //先进行读取流关闭操作,再执行删除,否则会报删除占用异常
            IOUtils.closeQuietly(ois);
            try {
                FileUtils.forceDelete(file);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

注:参考http://www.hollischuang.com/archives/1140

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注