-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSerialization.java
37 lines (31 loc) · 1.13 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
package com.yurii.salimov.lesson10.task05;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
/**
* @author Yuriy Salimov ([email protected])
* @version 1.0
*/
public final class Serialization {
public void serialize(final Object obj, final File file) throws IOException {
try (ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream(file))) {
output.writeObject(obj);
}
}
public void serialize(final Object obj, final String path) throws IOException {
final File file = new File(path);
serialize(obj, file);
}
public Object deserialize(final File file) throws IOException, ClassNotFoundException {
try (final ObjectInputStream input = new ObjectInputStream(new FileInputStream(file))) {
return input.readObject();
}
}
public Object deserialize(final String path) throws IOException, ClassNotFoundException {
final File file = new File(path);
return deserialize(file);
}
}