-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObjectWritingAndReadingDemo.java
executable file
·39 lines (30 loc) · 1.24 KB
/
ObjectWritingAndReadingDemo.java
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
import java.util.ArrayList;
import java.util.TreeSet;
public class ObjectWritingAndReadingDemo {
public static void main(String[] args) {
ArrayList<Integer> integerList = new ArrayList<Integer>();
TreeSet<String> stringSet = new TreeSet<String>();
integerList.add(5);
integerList.add(10);
integerList.add(20);
stringSet.add("five");
stringSet.add("ten");
stringSet.add("twenty");
/** Create a file called somefile.bla that ObjectWriter ow will write to. */
ObjectWriter ow = new ObjectWriter("somefile.bla");
ow.writeObject(integerList);
ow.writeObject(stringSet);
ObjectReader or = new ObjectReader("somefile.bla");
/* Read first object from the file. */
Object x = or.readObject();
/* Read second object from the file. */
Object y = or.readObject();
/* We happen to know that that first object is a list of integers,
* and the second object is a set of strings, so let's cast and
* print. */
ArrayList<Integer> xAsList = (ArrayList<Integer>) x;
TreeSet<String> yAsSet = (TreeSet<String>) y;
System.out.println(xAsList);
System.out.println(yAsSet);
}
}