企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# Java 序列化 > 原文: [https://beginnersbook.com/2014/07/java-serialization/](https://beginnersbook.com/2014/07/java-serialization/) 在这里,我们将讨论如何序列化和反序列化对象以及它的用途。 ## 什么是 Java 序列化? 序列化是一种将对象转换为字节流的机制,以便可以将其写入文件,通过网络传输或存储到数据库中。反序列化反之亦然。简单来说,序列化是将对象转换为字节流,反序列化是从字节流重建对象。 Java 序列化 API 执行序列化和反序列化。类必须实现`java.io.Serializable`接口才有资格进行序列化。 让我们举个例子来更好地理解这些概念: **示例** 此类实现`Serializable`接口,这意味着它可以被序列化。除了那些声明为`transient`的字段外,该类的所有字段都可以在转换为字节流后写入文件。在下面的示例中,我们有两个瞬态字段,这些字段不参与序列化。 `Student.java` ```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`扩展名。 ```java 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); } } } ``` 输出: ```java Serialzation Done!! ``` ## 对象的反序列化 从读取文件中的字节流后,该类将重建`Student`类的对象。观察此课程的输出,学生地址和学生身高字段为`null`和 0。这是因为这些字段在`Student`类中被声明为`transient`。 ```java 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()); } } ``` **输出:** ```java Student Name:Chaitanya Student Age:25 Student Roll No:101 Student Address:null Student Height:0 ```