Sunday 24 March 2019

Example of Java Deserialization .


Code:
?
1
2
3
4
5
6
7

import java.io.*; class Depersist{ public static void main(String args[])throws Exception{ ObjectInputStream in=new ObjectInputStream(new FileInputStream("f.txt")); Student s=(Student)in.readObject(); System.out.println(s.id+" "+s.name); in.close(); } }
Output:: 211 govind

Example of Java Serialization.

Description:
In this example, we are going to serialize the object of Student class.
The writeObject() method of ObjectOutputStream class provides the functionality to serialize the object.
We are saving the state of the object in the file named f.txt.

Code:
?
1
2
3
4
5
6
7

import java.io.*; class Persist{ public static void main(String args[])throws Exception{ Student s1 =new Student(211,"govind"); FileOutputStream fout=new FileOutputStream("f.txt"); ObjectOutputStream out=new ObjectOutputStream(fout); out.writeObject(s1); out.flush(); System.out.println("success"); } }
Output:: success