package com.zb.iolist;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.OutputStream;public class StudentSerial { public static void main(String[] args) throws Exception{ String file = "accessFile\\输入测试.txt"; //对象的序列化 ObjectOutputStream oos = new ObjectOutputStream( new FileOutputStream(file)); Student stu1 = new Student("1221010433", "嘿嘿", 24); oos.writeObject(stu1); oos.flush(); oos.close(); //以上程序会因为Student没有实现Serializable接口儿抛出异常 //对象的反序列化 ObjectInputStream ois = new ObjectInputStream( new FileInputStream(file)); Student stu2 = (Student) ois.readObject(); System.out.println(stu2); ois.close(); }}
添加回答
举报
0/150
提交
取消