File size: 1,307 Bytes
0284885
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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();
    }
}