zxy0307's picture
Upload 2300 files
0284885 verified
Raw History Blame Contribute Delete
1.31 kB
package case_package;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
public class Test2 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//1、创建反序列化流的对象
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("/Users/zhaoxiaoyong" +
"/Documents/Java/IO_stream_project/Object_stream_module/src/rough_draft/b.txt"));
//2、读取数据
Student s1 = (Student) ois.readObject();
Student s2 = (Student) ois.readObject();
Student s3 = (Student) ois.readObject();
//EOFException: Student s4 = (Student) ois.readObject();
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
//3、释放资源
ois.close();
//另一种方式:
ObjectInputStream ois2 = new ObjectInputStream(new FileInputStream("/Users/zhaoxiaoyong" +
"/Documents/Java/IO_stream_project/Object_stream_module/src/rough_draft/c.txt"));
ArrayList<Student> list = (ArrayList<Student>) ois2.readObject();
for (Student student : list) {
System.out.println(student);
}
ois2.close();
}
}