-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathSerialization.java
More file actions
53 lines (45 loc) · 1.59 KB
/
Serialization.java
File metadata and controls
53 lines (45 loc) · 1.59 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
50
51
52
53
package com.company;
import java.io.*;
/**
* Created by Vova Moskalenko on 04.06.2017.
*/
public class Serialization implements java.io.Serializable {
String name = "Marcus";
String city = "Roma";
int born = 121;
public static void main(String[] args) {
Serialization person = new Serialization();
//serialization
try {
FileOutputStream fileOut =
new FileOutputStream("person.txt");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(person);
out.close();
fileOut.close();
System.out.printf("Serialized data is saved in person.txt");
}catch(IOException i) {
i.printStackTrace();
}
Serialization person2 = null;
//deserialization
try {
FileInputStream fileIn = new FileInputStream("person.txt");
ObjectInputStream in = new ObjectInputStream(fileIn);
person2 = (Serialization) in.readObject();
in.close();
fileIn.close();
}catch(IOException i) {
i.printStackTrace();
return;
}catch(ClassNotFoundException c) {
System.out.println("Serialization class not found");
c.printStackTrace();
return;
}
System.out.println("Deserialized person...");
System.out.println("Name: " + person.name);
System.out.println("City: " + person.city);
System.out.println("Born: " + person.born);
}
}