Serialization and Deserialization process in java?
package javaio;
import java.io.Serializable;
public class Employee implements Serializable{
String EmpName;
String EmpAdd;
int EmpId;
String Empcompnay;
public String getEmpName() {
return EmpName;
}
public void setEmpName(String empName) {
EmpName = empName;
}
public String getEmpAdd() {
return EmpAdd;
}
public void setEmpAdd(String empAdd) {
EmpAdd = empAdd;
}
public int getEmpId() {
return EmpId;
}
public void setEmpId(int empId) {
EmpId = empId;
}
public String getEmpcompnay() {
return Empcompnay;
}
public void setEmpcompnay(String empcompnay) {
Empcompnay = empcompnay;
}
}
================================================================================
package javaio;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class EmpSerialize {
public void WriteEmployeeData() throws Exception
{
Employee e1=new Employee();
e1.setEmpName("govindkhan");
e1.setEmpAdd("Madhubani-Bihar");
e1.setEmpId(1001);
e1.setEmpcompnay("xyz");
FileOutputStream fos=new FileOutputStream("abc.txt");
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(e1);
System.out.println("data are serialized successfully");
}
public void ReadEmployeeData() throws Exception
{
FileInputStream fis=new FileInputStream("abc.txt");
ObjectInputStream ois=new ObjectInputStream(fis);
Employee e2=(Employee)ois.readObject();
System.out.println(e2.getEmpName()+" "+e2.getEmpAdd()+" "+e2.getEmpId()+" "+e2.getEmpcompnay());
System.out.println("data are deserialized successfully");
}
}
===============================================================================
package javaio;
public class EmployeeDemo {
public static void main(String[] args) throws Exception {
EmpSerialize es=new EmpSerialize();
es.WriteEmployeeData();
es.ReadEmployeeData();
}
}
package javaio;
import java.io.Serializable;
public class Employee implements Serializable{
String EmpName;
String EmpAdd;
int EmpId;
String Empcompnay;
public String getEmpName() {
return EmpName;
}
public void setEmpName(String empName) {
EmpName = empName;
}
public String getEmpAdd() {
return EmpAdd;
}
public void setEmpAdd(String empAdd) {
EmpAdd = empAdd;
}
public int getEmpId() {
return EmpId;
}
public void setEmpId(int empId) {
EmpId = empId;
}
public String getEmpcompnay() {
return Empcompnay;
}
public void setEmpcompnay(String empcompnay) {
Empcompnay = empcompnay;
}
}
================================================================================
package javaio;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class EmpSerialize {
public void WriteEmployeeData() throws Exception
{
Employee e1=new Employee();
e1.setEmpName("govindkhan");
e1.setEmpAdd("Madhubani-Bihar");
e1.setEmpId(1001);
e1.setEmpcompnay("xyz");
FileOutputStream fos=new FileOutputStream("abc.txt");
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(e1);
System.out.println("data are serialized successfully");
}
public void ReadEmployeeData() throws Exception
{
FileInputStream fis=new FileInputStream("abc.txt");
ObjectInputStream ois=new ObjectInputStream(fis);
Employee e2=(Employee)ois.readObject();
System.out.println(e2.getEmpName()+" "+e2.getEmpAdd()+" "+e2.getEmpId()+" "+e2.getEmpcompnay());
System.out.println("data are deserialized successfully");
}
}
===============================================================================
package javaio;
public class EmployeeDemo {
public static void main(String[] args) throws Exception {
EmpSerialize es=new EmpSerialize();
es.WriteEmployeeData();
es.ReadEmployeeData();
}
}
No comments:
Post a Comment