

package com.io.liushuaishuai;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class ObjectOutputStreamDemo01 {
public static void main(String[] args) throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("myIOstream\\copy02.java"));
Student s1 = new Student("林青霞", 99, 50, 80);
oos.writeObject(s1);
/*
NotSerializableException:抛出一个实例需要一个Serializable接口。
序列化运行时或实例的类可能会抛出此异常。 参数应该是类的名称。
Serializable类的序列化由实现java.io.Serializable接口的类启用。
不实现此接口的类将不会使任何状态序列化或反序列化。
可序列化类的所有子类型都是可序列化的。
序列化接口没有方法或字段,仅用于标识可串行化的语义
*/
oos.close();
}
}

package com.io.liushuaishuai;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
public class ObjectInputStreamDemo {
public static void main(String[] args) throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("myIOstream\\copy02.java"));
Student s = (Student) ois.readObject();
System.out.println(s.getName() + " " + s.getChinese() + " " + s.getMath() + " " + s.getEnglish());
ois.close();
}
}
原文:https://www.cnblogs.com/lsswudi/p/11431257.html