-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerializationDeserializationDemoOne.java
More file actions
73 lines (53 loc) · 1.78 KB
/
Copy pathSerializationDeserializationDemoOne.java
File metadata and controls
73 lines (53 loc) · 1.78 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package Phase3_ObjectOrientation.SerializationDeserialization;
import java.io.*;
public class SerializationDeserializationDemoOne {
public static void main(String[] args)
{
DemoOneModel object = new DemoOneModel(1, "Deepak Java");
String filename = "file.zip";
// Serialization
try
{
//Saving of object in a file
FileOutputStream file = new FileOutputStream(filename);
ObjectOutputStream out = new ObjectOutputStream(file);
// Method for serialization of object
out.writeObject(object);
out.close();
file.close();
System.out.println("Object has been serialized");
}
catch(IOException ex)
{
System.out.println("IOException is caught");
}
DemoOneModel object1 = null;
// Deserialization
try
{
// Reading the object from a file
FileInputStream file = new FileInputStream(filename);
ObjectInputStream in = new ObjectInputStream(file);
// Method for deserialization of object
object1 = (DemoOneModel) in.readObject();
in.close();
file.close();
System.out.println("Object has been deserialized ");
System.out.println("a = " + object1.value);
System.out.println("b = " + object1.key);
}
catch(IOException ex)
{
System.out.println("IOException is caught");
}
catch(ClassNotFoundException ex)
{
System.out.println("ClassNotFoundException is caught");
}
// OUTPUT
// Object has been serialized
// Object has been deserialized
// a = 1
// b = Deepak Java
}
}