-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrderOfSerialization.java
More file actions
40 lines (35 loc) · 1.06 KB
/
Copy pathOrderOfSerialization.java
File metadata and controls
40 lines (35 loc) · 1.06 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
// Serializing Multiple Objects to the same file
import java.io.*;
class Dog implements Serializable{
int a=10;
transient int b =20;
}
class Cat implements Serializable{
transient int c =30;
int d = 40;
}
class Rat implements Serializable{
transient int e =50;
transient static int f= 60;
}
class OrderOfSerialization{
public static void main(String[] args) throws Exception{
Dog d1 = new Dog();
Cat c1 = new Cat();
Rat r1 = new Rat();
FileOutputStream fos = new FileOutputStream("bbb.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(d1);
oos.writeObjct(c1);
oos.writeObject(r1);
FileInputStream fis = new FileInputStream("bbb.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
// In which order we serialized in the very same order we have to de-serialize.
Dog d2 = (Dog)ois.readObject();
Cat c2 = (Cat)ois.readObject();
Rat r2 = (Rat)ois.readObject();
System.out.println(d2.a+"......."+d2.b);
System.out.println(c2.c+"......."+c2.d);
System.out.println(r2.e+"......."+r2.f);
}
}