Java 序列化

Java 序列化,在这里,我们将讨论如何序列化和反序列化对象以及它的用途。

什么是 Java 序列化

序列化是一种将对象转换为字节流的机制,以便可以将其写入文件,通过网络传输或存储到数据库中。反序列化反之亦然。简单来说,序列化是将对象转换为字节流,反序列化是从字节流重建对象。 Java 序列化 API 执行序列化和反序列化。类必须实现java.io.Serializable接口才有资格进行序列化。

让我们举个例子来更好地理解这些概念:

示例

此类实现Serializable接口,这意味着它可以被序列化。除了那些声明为transient的字段外,该类的所有字段都可以在转换为字节流后写入文件。在下面的示例中,我们有两个瞬态字段,这些字段不参与序列化。

Student.java

public class Student implements java.io.Serializable{

private int stuRollNum;

private int stuAge;

private String stuName;

private transient String stuAddress;

private transient int stuHeight;

public Student(int roll, int age, String name,

String address, int height) {

this.stuRollNum = roll;

this.stuAge = age;

this.stuName = name;

this.stuAddress = address;

this.stuHeight = height;

}

public int getStuRollNum() {

return stuRollNum;

}

public void setStuRollNum(int stuRollNum) {

this.stuRollNum = stuRollNum;

}

public int getStuAge() {

return stuAge;

}

public void setStuAge(int stuAge) {

this.stuAge = stuAge;

}

public String getStuName() {

return stuName;

}

public void setStuName(String stuName) {

this.stuName = stuName;

}

public String getStuAddress() {

return stuAddress;

}

public void setStuAddress(String stuAddress) {

this.stuAddress = stuAddress;

}

public int getStuHeight() {

return stuHeight;

}

public void setStuHeight(int stuHeight) {

this.stuHeight = stuHeight;

}

}

对象的序列化

该类正在将Student类的对象写入Student.ser文件。我们使用FileOutputStream和ObjectOutputStream将对象写入File。

注意:根据 Java 序列化的最佳实践,文件名应具有.ser扩展名。

import java.io.FileOutputStream;

import java.io.ObjectOutputStream;

import java.io.IOException;

public class SendClass

{

public static void main(String args[])

{

Student obj = new Student(101, 25, "Chaitanya", "Agra", 6);

try{

FileOutputStream fos = new FileOutputStream("Student.ser");

ObjectOutputStream oos = new ObjectOutputStream(fos);

oos.writeObject(obj);

oos.close();

fos.close();

System.out.println("Serialzation Done!!");

}catch(IOException ioe){

System.out.println(ioe);

}

}

}

输出:

Serialzation Done!!

对象的反序列化

从读取文件中的字节流后,该类将重建Student类的对象。观察此课程的输出,学生地址和学生身高字段为null和 0。这是因为这些字段在Student类中被声明为transient。

import java.io.FileInputStream;

import java.io.ObjectInputStream;

import java.io.IOException;

public class AcceptClass {

public static void main(String args[])

{

Student o=null;

try{

FileInputStream fis = new FileInputStream("Student.ser");

ObjectInputStream ois = new ObjectInputStream(fis);

o = (Student)ois.readObject();

ois.close();

fis.close();

}

catch(IOException ioe)

{

ioe.printStackTrace();

return;

}catch(ClassNotFoundException cnfe)

{

System.out.println("Student Class is not found.");

cnfe.printStackTrace();

return;

}

System.out.println("Student Name:"+o.getStuName());

System.out.println("Student Age:"+o.getStuAge());

System.out.println("Student Roll No:"+o.getStuRollNum());

System.out.println("Student Address:"+o.getStuAddress());

System.out.println("Student Height:"+o.getStuHeight());

}

}

输出:

Student Name:Chaitanya

Student Age:25

Student Roll No:101

Student Address:null

Student Height:0