logo
Java Serialization
Serialization
The process of saving an object to a file (or) the process of sending an object across the network is called serialization.
But strictly speaking the process of converting the object from java supported form to the network supported form of file supported form.
To do the serialization we required fallowing classes
  1. FileOutputStream
  2. ObjectOutputStream
To perform serialization :- we are writing the object data to the file called abc.txt file we are transferring that file across the network.
import java.io.Serializable; 
public class Student implements Serializable 
{ 
int id; 
String name; 
int marks; 
public Student(int id, String name,int marks) 
{ 
this.id = id; 
this.name = name; 
this.marks=marks; 
} 
}

import java.io.*; 
class Serializable1 
{ 
public static void main(String args[])throws Exception 
{ 
Student s1 =new Student(211,"ravi",100); 
FileOutputStream fos=new FileOutputStream("abc.txt",true); 
ObjectOutputStream oos=new ObjectOutputStream(fos); 
oos.writeObject(s1); 
oos.flush(); 
System.out.println("Serializable process success"); 
} 
}
Output :
abc.txt write objuct
To perform deserialization:- in the network the file is available with java data to read the data we have to go for deserialization.
class Deserialization 
{ 
public static void main(String args[])throws Exception 
{ 
//deserialization process 
FileInputStream fis=new FileInputStream("abc.txt"); 
ObjectInputStream ois=new ObjectInputStream(fis); 
Student s=(Student)ois.readObject(); 
System.out.println("the student name is:"+s.name); 
System.out.println("the stuent id is:"+s.id); 
System.out.println("the student marks:"+s.marks); 
System.out.println("deserialization success"); 
} 
}
Output :
get in said file data