Java序列化教程
1. 简介
在Java编程中,序列化是指将对象转换为字节流的过程,以便在网络上传输或将其保存到磁盘上。反序列化则是将字节流转换回对象的过程。本文将教会你如何在Java中进行序列化。
2. 序列化的流程
下面是Java序列化的整个流程,通过表格展示了每个步骤所需的代码和注释。
步骤 | 代码 | 注释 |
---|---|---|
1. 创建要序列化的类 | java public class Employee implements Serializable { private String name; private String address; private transient int age; } |
创建一个包含需要序列化的属性的类。注意,需要实现Serializable 接口。对于不希望被序列化的属性,可以使用transient 关键字修饰 |
2. 序列化对象 | java try { FileOutputStream fileOut = new FileOutputStream("employee.ser"); ObjectOutputStream out = new ObjectOutputStream(fileOut); out.writeObject(employee); out.close(); fileOut.close(); } catch (IOException i) { i.printStackTrace(); } |
创建FileOutputStream 和ObjectOutputStream 对象,将对象写入文件。employee 是需要序列化的对象 |
3. 反序列化对象 | java try { FileInputStream fileIn = new FileInputStream("employee.ser"); ObjectInputStream in = new ObjectInputStream(fileIn); Employee employee = (Employee) in.readObject(); in.close(); fileIn.close(); } catch (IOException i) { i.printStackTrace(); } catch (ClassNotFoundException c) { System.out.println("Employee class not found"); c.printStackTrace(); } |
创建FileInputStream 和ObjectInputStream 对象,从文件中读取对象。需要使用readObject() 方法将对象转换回原始类型 |
4. 使用序列化后的对象 | java System.out.println("Name: " + employee.getName()); System.out.println("Address: " + employee.getAddress()); System.out.println("Age: " + employee.getAge()); |
使用反序列化后的对象,访问其属性和方法 |
3. 类图
下面是使用mermaid语法绘制的类图,显示了Employee
类的属性和方法:
classDiagram
class Employee {
- name: String
- address: String
- age: int
+ getName(): String
+ getAddress(): String
+ getAge(): int
}
4. 状态图
下面是使用mermaid语法绘制的状态图,展示了Employee
对象的状态变化:
stateDiagram
[*] --> Uninitialized
Uninitialized --> Serialized: Serialization
Serialized --> Deserialized: Deserialization
Deserialized --> [*]
5. 完整示例代码
下面是完整的示例代码,包括了定义Employee
类、序列化和反序列化对象以及使用对象的代码:
import java.io.*;
public class SerializationExample {
public static void main(String[] args) {
// Step 1: Create an Employee object
Employee employee = new Employee();
employee.setName("John Doe");
employee.setAddress("123 Main St");
employee.setAge(30);
// Step 2: Serialize the object
try {
FileOutputStream fileOut = new FileOutputStream("employee.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(employee);
out.close();
fileOut.close();
System.out.println("Serialized data is saved in employee.ser");
} catch (IOException i) {
i.printStackTrace();
}
// Step 3: Deserialize the object
try {
FileInputStream fileIn = new FileInputStream("employee.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
Employee deserializedEmployee = (Employee) in.readObject();
in.close();
fileIn.close();
System.out.println("Deserialized data:");
System.out.println("Name: " + deserializedEmployee.getName());
System.out.println("Address: " + deserializedEmployee.getAddress());
System.out.println("Age: " + deserializedEmployee.getAge());
} catch (IOException i) {
i.printStackTrace();
} catch (ClassNotFoundException c) {
System.out.println("Employee class not found");
c.printStackTrace();
}
}
}
class Employee implements Serializable {
private String name;
private String address;
private transient int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getAge() {
return age;