/
SerializeUtils.java
68 lines (62 loc) · 2 KB
/
SerializeUtils.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package cn.trinea.android.common.util;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
/**
* Serialize Utils
*
* @author <a href="http://www.trinea.cn" target="_blank">Trinea</a> 2012-5-14
*/
public class SerializeUtils {
private SerializeUtils() {
throw new AssertionError();
}
/**
* Deserialization object from file.
*
* @param filePath file path
* @return de-serialized object
* @throws RuntimeException if an error occurs
*/
public static Object deserialization(String filePath) {
ObjectInputStream in = null;
try {
in = new ObjectInputStream(new FileInputStream(filePath));
Object o = in.readObject();
in.close();
return o;
} catch (FileNotFoundException e) {
throw new RuntimeException("FileNotFoundException occurred. ", e);
} catch (ClassNotFoundException e) {
throw new RuntimeException("ClassNotFoundException occurred. ", e);
} catch (IOException e) {
throw new RuntimeException("IOException occurred. ", e);
} finally {
IOUtils.close(in);
}
}
/**
* Serialize object to file.
*
* @param filePath file path
* @param obj object
* @throws RuntimeException if an error occurs
*/
public static void serialization(String filePath, Object obj) {
ObjectOutputStream out = null;
try {
out = new ObjectOutputStream(new FileOutputStream(filePath));
out.writeObject(obj);
out.close();
} catch (FileNotFoundException e) {
throw new RuntimeException("FileNotFoundException occurred. ", e);
} catch (IOException e) {
throw new RuntimeException("IOException occurred. ", e);
} finally {
IOUtils.close(out);
}
}
}