Serialization in java

Serialization in java

Serialization is the process of converting an object state to byte stream.if we say any object is serializable if its class or super class implements Serializable interface.Serializable interface present in java.io package.Serializable interface is a marker interface

We can implement serialization by using FileOutPutStreamand ObjectOutputStream

Example


package com.java2study;

import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class Student implements Serializable {

private static final long serialVersionUID = 1L;

private int studentId;
private String studentName;
private double fee;
public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}

public double getFee() {
return fee;
}
public void setFee(double fee) {
this.fee = fee;
}
public static long getSerialversionuid() {
return serialVersionUID;
}

public static void main(String[] args) throws Exception
{
Student s = new Student();
s.setStudentId(1);
s.setStudentName("ABC");
s.setFee(1400);
FileOutputStream fos = new FileOutputStream("student.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(s);
System.out.println("successfully data stored to student.txt");

}

}

output


successfully data stored to student.txt

-->