forked from McGillAISociety/mais-202-coding-challenge-w2019
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PersistenceObjectStream.java
46 lines (37 loc) · 1.01 KB
/
PersistenceObjectStream.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
40
41
42
43
44
45
package ca.mcgill.ecse223.block.persistence;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class PersistenceObjectStream {
private static String filename = "output.txt";
public static void serialize(Object object) {
FileOutputStream fileOut;
try {
fileOut = new FileOutputStream(filename);
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(object);
out.close();
fileOut.close();
} catch (Exception e) {
throw new RuntimeException("Could not save data to file '" + filename + "'.");
}
}
public static Object deserialize() {
Object o = null;
ObjectInputStream in;
try {
FileInputStream fileIn = new FileInputStream(filename);
in = new ObjectInputStream(fileIn);
o = in.readObject();
in.close();
fileIn.close();
} catch (Exception e) {
o = null;
}
return o;
}
public static void setFilename(String newFilename) {
filename = newFilename;
}
}