-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileUtil.java
More file actions
49 lines (43 loc) · 1.43 KB
/
FileUtil.java
File metadata and controls
49 lines (43 loc) · 1.43 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package gitlet;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
/* Functions to deserialize and serialize files */
public class FileUtil {
public static void serialize(File outFile, Object obj) {
try {
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(outFile));
out.writeObject(obj);
out.close();
} catch (IOException excp) {
excp.printStackTrace();
}
}
public static Object deSerialize(File inFile) {
try {
ObjectInputStream inp = new ObjectInputStream(new FileInputStream(inFile));
Object obj = inp.readObject();
inp.close();
return obj;
} catch (IOException | ClassNotFoundException excp) {
excp.printStackTrace();
}
return null;
}
public static byte[] changeToByteArr(Object obj) {
try {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
ObjectOutputStream objectStream = new ObjectOutputStream(stream);
objectStream.writeObject(obj);
objectStream.close();
return stream.toByteArray();
} catch (IOException excp) {
excp.printStackTrace();
}
return null;
}
}