Serializable

1.运行环境

jdk1.8.0_77 Intellij IDEA2018.3 x64

2.理解什么是Serializable

Serializable就是将对象“冷冻”的技术。

3.了解Serializable原理

编码实现一个类的序列化

关键代码

public static void main(String[] args) throws Exception{

Student stu = new Student();

File file = new File("bb.dat");

ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));

oos.writeObject(stu);

}

从上例中可以看出,我们将一个对象进行冷冻,是将其写到一个文件中

编码实现一个反序列化

关键代码

public static void main(String[] args) throws Exception{

File file = new File("bb.dat");

ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));

Object obj = ois.readObject();

System.out.println(obj.hashCode());

}

从上例中可以看出,对象的反序列化(解冻),实际上就是将对象从文件中读到内存中

4.心得

好记性不如烂笔头,反复的敲打代码,对于我的熟练度的确提升很大。

原文地址:https://www.cnblogs.com/nanren/p/13603591.html