-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathObjectWriter.java
executable file
·32 lines (28 loc) · 962 Bytes
/
ObjectWriter.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
import java.io.Serializable;
import java.io.ObjectOutputStream;
import java.io.FileOutputStream;
/** Utility class for writing Java objects to files. The Object must
* implement the Serializable interface. */
public class ObjectWriter {
private FileOutputStream fos;
private ObjectOutputStream os;
/** Creates a new ObjectWriter. */
public ObjectWriter(String filename) {
try {
fos = new FileOutputStream(filename, false);
os = new ObjectOutputStream(fos);
} catch (java.io.IOException e) {
System.out.println("Error creating ObjectWriter: ");
e.printStackTrace();
}
}
/** Writes an object to a file. */
public void writeObject(Serializable s) {
try {
os.writeObject(s);
} catch (java.io.IOException e) {
System.out.println("Error writing object: ");
e.printStackTrace();
}
}
}