File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change 1+ import java .io .*;
2+
3+ class Student implements Serializable {
4+ String name ;
5+ int age ;
6+
7+ Student (String name , int age ) {
8+ this .name = name ;
9+ this .age = age ;
10+ }
11+ }
12+
13+ public class SerializationDemo {
14+ public static void main (String [] args ) {
15+ try {
16+ Student student = new Student ("Alice" , 20 );
17+
18+ // Serialization
19+ ObjectOutputStream out = new ObjectOutputStream (new FileOutputStream ("student.ser" ));
20+ out .writeObject (student );
21+ out .close ();
22+
23+ // Deserialization
24+ ObjectInputStream in = new ObjectInputStream (new FileInputStream ("student.ser" ));
25+ Student deserializedStudent = (Student ) in .readObject ();
26+ in .close ();
27+
28+ System .out .println ("Deserialized Student: Name - " + deserializedStudent .name + ", Age - " + deserializedStudent .age );
29+ } catch (IOException | ClassNotFoundException e ) {
30+ e .printStackTrace ();
31+ }
32+ }
33+ }
You can’t perform that action at this time.
0 commit comments