-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDemo03SerializeCollectionOfObjects.java
More file actions
29 lines (27 loc) · 1.09 KB
/
Demo03SerializeCollectionOfObjects.java
File metadata and controls
29 lines (27 loc) · 1.09 KB
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
package Demo14;
import java.io.*;
import java.util.ArrayList;
public class Demo03SerializeCollectionOfObjects {
public static void main(String[] args) throws IOException, ClassNotFoundException {
ArrayList<Object> ppl = new ArrayList<>();
ppl.add(new Person("Alice", 18));
ppl.add(new Person("Bruce",20));
ppl.add(new Person("Carmen",24));
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("src\\Demo14\\PersonList.txt"));
/*for (Object o : ppl) {
oos.writeObject(o);
}*/
oos.writeObject(ppl);
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("src\\Demo14\\PersonList.txt"));
//ArrayList<Object> ppl_read = new ArrayList<>();
//System.out.println(ois.readObject().toString());
//ppl_read.add(ois.readObject());
Object o = ois.readObject();
ArrayList<Person> list = (ArrayList<Person>)o;
for (Person person : list) {
System.out.println(person);
}
oos.close();
ois.close();
}
}