public class Main {
public static void main(String[] args) throws IOException, ClassNotFoundException {
final Person person = new Person();
person.setAge(18);
person.setName("汤姆");
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(person);
byte[] bytes = byteArrayOutputStream.toByteArray();
ObjectInputStream objectInputStream = new ObjectInputStream(new ByteArrayInputStream(bytes));
Person person2 = (Person) objectInputStream.readObject();
System.out.println(person2);
}
@Data
static class Person implements Serializable {
private String name;
private Integer age;
}
}