Core Java Tutorial

 
 

Serialization & de-serialization

Java Object serialization is completly JVM independent i.e you can serialize one object in one OS(Platform) and you can de-serialize same object in other OS(Platform).Object serialization is converting java Object to sequence of byte streams and if you do the exactly opposite to this process hten you call that as a de-serialization(i.e converting sequence of byte streams into Java Objects.In order to perform serialization or de-serialization you must implement java.io.serializable interface(it will tell JVM that this class can be serializable.We will use this concept to persist data into Database , files ....

Serialization

Serialization

The process of converting java Objects into sequence of bytes is Serialization.We will use ObjectOuputStream class methods for the serialization concept.Here is the Demo for serialization, follow these steps to create sample serialization java project.

  1. create a package name with test_serialization
  2. create a class in that class with name Student.java
  3. create another class with name SerializationDemo.java

Student.java

package test_serialization;

import java.io.Serializable;

public class Student  implements Serializable{
	
	String stName;
	int stSSN; // for security purpose use transient key word
	String stNickName;
	@Override
	public String toString() {
		return "Student [stName=" + stName + ", stSSN=" + stSSN + ", stNickName=" + stNickName + "]";
	}

}
Note:In any class if you don't want to see your SSN(Anythinhg that you don't want to share it to others, in this example SSN, but it may  be anything ) after serialization use 
Transient keyword before that field.

SerializationDemo.java

package test_serialization; import java.io.FileOutputStream; import java.io.ObjectOutputStream; public class SerializationDemo { public static void main(String[] args) { Student student = new Student(); student.stName = "Hello"; student.stSSN= 1111; student.stNickName = "Hel"; try{ FileOutputStream fos = new FileOutputStream("C:\\Users\\name\\Desktop\\sample.txt"); ObjectOutputStream dos = new ObjectOutputStream(fos); dos.writeObject(student); System.out.println("Serialized success Fully"); dos.close(); fos.close(); }catch(Exception e){ e.printStackTrace(); } } } Output: Serialized success Fully. Now go ahead and look the file you gave in above path.

De-Serialization

The process of converting byte streams into object.We will use ObjectInputStream class methods for de-serialization concept.Here is the Demo for de-serialization, follow these steps to create sample de-serialization java project.

  1. create a package name with test_de_serialization
  2. create a class in that class with name Student.java
  3. create another class with name DeSerializationDemo.java

DeSerializationDemo.java

package test_serialization;

import java.io.FileInputStream;
import java.io.ObjectInputStream;

public class DeSerializationDemo {
	
public static void main(String[] args) {
		
		Student student = null;
		
		try{
			 FileInputStream fis = new FileInputStream("C:\\Users\\venka\\Desktop\\kallam.txt");
			 ObjectInputStream ois = new ObjectInputStream(fis);
			 student =(Student)ois.readObject();
			 System.out.println("DeSerialized success Fully");
			 System.out.println(student);
			 ois.close();
			 fis.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		
		
		
	}

}

Output: DeSerialized success Fully Student [stName= Hello, stSSN=1111, stNickName=Hel]