√ JavaSE - 24.怎么传输对象(卷2 P70)

  1. 利用ObjectOutputStream类进行对象序列化,ObjectInputStream类进行对象反序列化。
  2. 实现序列化的对象必须实现Serializable接口,提供全局常量serialVersionUID,而且其所有属性也是可序列化的。
  3. 利用对象序列化可以快速地对一个对象进行深拷贝。
import java.io.*;

public class SerializableTest {
    public static void main(String[] args) {
        //对象序列化
        ObjectOutputStream outputStream = null;
        try {
            outputStream = new ObjectOutputStream(new FileOutputStream("D:\\Desktop\\Files\\Object.dat", true));
            outputStream.writeObject(new Test1("Test1", 1, new Test2("Test2", 2)));
            outputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        //对象反序列化
        ObjectInputStream inputStream = null;
        try {
            inputStream = new ObjectInputStream(new FileInputStream("D:\\Desktop\\Files\\Object.dat"));
            Test1 test1 = (Test1) inputStream.readObject();
            System.out.println(test1);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        //对象深拷贝
        try {
            Test1 test1 = new Test1("Test1", 10, new Test2("Test2", 20));
            ByteArrayOutputStream data = new ByteArrayOutputStream();
            outputStream = new ObjectOutputStream(data);
            outputStream.writeObject(test1);
            inputStream = new ObjectInputStream(new ByteArrayInputStream(data.toByteArray()));
            Test1 test2 = (Test1) inputStream.readObject();
            System.out.println(test1);
            System.out.println(test2);
            System.out.println(test1 == test2);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

class Test1 implements Serializable {
    private static final long serialVersionUID = -7300046014501169240L;
    private String str;
    private int value;
    private Test2 test2;

    public Test1(String str, int value, Test2 test2) {
        this.str = str;
        this.value = value;
        this.test2 = test2;
    }

    @Override
    public String toString() {
        return "Test1{" +
                "str='" + str + '\'' +
                ", value=" + value +
                ", test2=" + test2 +
                '}';
    }
}

class Test2 implements Serializable {
    private static final long serialVersionUID = 3341032494948326316L;
    private String str;
    private int value;

    public Test2(String str, int value) {
        this.str = str;
        this.value = value;
    }

    @Override
    public String toString() {
        return "Test2{" +
                "str='" + str + '\'' +
                ", value=" + value +
                '}';
    }
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值