Java 提供了多种序列化的方式,其中最常用的是使用 Java 内置的序列化机制,即通过实现 java.io.Serializable
接口来实现对象的序列化和反序列化。下面我将介绍这种方式以及其他一些常见的序列化方式,并附上示例代码。
步骤流程:
java.io.Serializable
接口。ObjectOutputStream
将对象序列化到输出流中。ObjectInputStream
从输入流中反序列化对象。示例代码:
import java.io.*;
class Person implements Serializable {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// Getters and setters
}
public class SerializationExample {
public static void main(String[] args) {
Person person = new Person("Alice", 30);
try {
// Serialization
FileOutputStream fileOut = new FileOutputStream("person.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(person);
out.close();
fileOut.close();
// Deserialization
FileInputStream fileIn = new FileInputStream("person.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
Person deserializedPerson = (Person) in.readObject();
in.close();
fileIn.close();
System.out.println("Deserialized Person: " + deserializedPerson.getName() + ", " + deserializedPerson.getAge());
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
步骤流程:
示例代码(使用 Jackson 库):
import com.fasterxml.jackson.databind.ObjectMapper;
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// Getters and setters
}
public class JsonSerializationExample {
public static void main(String[] args) {
ObjectMapper objectMapper = new ObjectMapper();
Person person = new Person("Bob", 25);
try {
// Serialization
String jsonString = objectMapper.writeValueAsString(person);
System.out.println("JSON: " + jsonString);
// Deserialization
Person deserializedPerson = objectMapper.readValue(jsonString, Person.class);
System.out.println("Deserialized Person: " + deserializedPerson.getName() + ", " + deserializedPerson.getAge());
} catch (Exception e) {
e.printStackTrace();
}
}
}
步骤流程:
示例代码(使用 JAXB):
import javax.xml.bind.*;
import java.io.StringWriter;
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// Getters and setters
}
public class XmlSerializationExample {
public static void main(String[] args) {
try {
// Serialization
Person person = new Person("Charlie", 28);
JAXBContext context = JAXBContext.newInstance(Person.class);
Marshaller marshaller = context.createMarshaller();
StringWriter writer = new StringWriter();
marshaller.marshal(person, writer);
String xmlString = writer.toString();
System.out.println("XML: " + xmlString);
// Deserialization
String xml = "<person><age>28</age><name>Charlie</name></person>";
Unmarshaller unmarshaller = context.createUnmarshaller();
Person deserializedPerson = (Person) unmarshaller.unmarshal(new StringReader(xml));
System.out.println("Deserialized Person: " + deserializedPerson.getName() + ", " + deserializedPerson.getAge());
} catch (Exception e) {
e.printStackTrace();
}
}
}
这些是 Java 中常见的序列化方式。根据你的需求和项目特点,选择适合的方式来实现对象的序列化和反序列化。