-
Notifications
You must be signed in to change notification settings - Fork 1
/
Serialization.java
39 lines (33 loc) · 1.17 KB
/
Serialization.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
package com.yurii.salimov.lesson09.task04;
import java.io.*;
/**
* @author Yuriy Salimov ([email protected])
* @version 1.0
*/
public final class Serialization {
public void serialize(final Object obj, final File file) {
try (FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream output = new ObjectOutputStream(fos)) {
output.writeObject(obj);
} catch (IOException ex) {
System.out.println("ERROR save object!");
}
}
public void serialize(final Object obj, final String path) {
serialize(obj, new File(path));
}
public Object deserialize(final File file) {
Object obj = null;
try (FileInputStream fis = new FileInputStream(file);
ObjectInputStream input = new ObjectInputStream(fis)) {
obj = input.readObject();
} catch (IOException | ClassNotFoundException | ExceptionInInitializerError ex) {
System.out.println("ERROR load object!");
file.delete();
}
return obj;
}
public Object deserialize(final String path) {
return deserialize(new File(path));
}
}