-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExternalizableDemo.java
More file actions
40 lines (34 loc) · 1.27 KB
/
Copy pathExternalizableDemo.java
File metadata and controls
40 lines (34 loc) · 1.27 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
// program to illustrate externalization(saving only part of the object to the file) in java
import java.io.*;
class ExternalizableDemo implements Externalizable{ // Externalizable extends Serializable
String s1;
int i;
String s2;
public ExternalizableDemo(){
System.out.println("public no-argument constuctor called");
}
public ExternalizableDemo(String s1 ,int i ,String s2){
this.s1 = s1;
this.i = i;
this.s2 = s2;
}
//Following are the two methods present in Externalizable interface
public void writeExternal(ObjectOutput out) throws IOException{
out.writeObject(s1);
out.writeInt(i);
}
public void readExternal(ObjectInput in) throws IOException,ClassNotFoundException{
s1 =(String)in.readObject();
i = in.readInt();
}
public static void main(String[] args) throws Exception{
ExternalizableDemo ob1 =new ExternalizableDemo("Fifa World Cup", 2018, "Russia");
FileOutputStream fos = new FileOutputStream("abc.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(ob1);
FileInputStream fis = new FileInputStream("abc.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
ExternalizableDemo ob2 =(ExternalizableDemo)ois.readObject();
System.out.println(ob2.s1+"......"+ob2.i+"......."+ob2.s2);
}
}