-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWriteObjects.java
More file actions
36 lines (26 loc) · 860 Bytes
/
Copy pathWriteObjects.java
File metadata and controls
36 lines (26 loc) · 860 Bytes
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
package tutorial35;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Arrays;
public class WriteObjects {
public static void main(String[] args) {
System.out.println("Writing arrays of objects..");
Person[] people = { new Person(1, "june"), new Person(2, "may"), new Person(3, "april"), new Person(4, "dec") };
ArrayList<Person> peopleList = new ArrayList<Person>(Arrays.asList(people));
try {
FileOutputStream fs = new FileOutputStream("people.ser");
ObjectOutputStream os = new ObjectOutputStream(fs);
os.writeObject(people);
os.writeObject(peopleList);
os.writeInt(peopleList.size());
for (Person person : peopleList) {
os.writeObject(person);
}
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}