Bootstrap

Java笔记 IO —— 序列化和反序列化,java实现区块链完整教程

package review.IO;

import java.io.*;

public class Demo14 {

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

//写方法,将对象存入到文件中,也就是将对象进行持久化

write();

//读方法:把文本文件中的流对象数据或者网络中的流数据给还原成一个对象

read();

}

public static void read() throws Exception {

//ObjectInputStream(InputStream in)

//创建从指定的InputStream读取的ObjectInputStream

ObjectInputStream ois = new ObjectInputStream(new FileInputStream(“obj.txt”));

//Object readObject()

//从ObjectInputStream读取一个对象

Object obj = ois.readObject();

//向下转型,从而使用Person类中的方法

Person p = (Person)obj;

System.out.println(p.getName()+"—"+p.getAge());

ois.close();

}

public static void write() throws IOException {

//ObjectOutputStream(OutputStream out)

// 创建一个写入指定的OutputStream的ObjectOutputStream

;