public static void main(String[] args) throws IOException, ClassNotFoundException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(new Person("zhangsan", 20, LocalDate.of(2000, 10, 20)));
byte[] zhangsanByte = byteArrayOutputStream.toByteArray();
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(zhangsanByte);
ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
final Person o = (Person)objectInputStream.readObject();
System.out.println(o.toString());
}
private static class Person implements Serializable{
private String name;
private Integer age;
private LocalDate birthday;
public Person(String name, Integer age, LocalDate birthday) {
this.name = name;
this.age = age;
this.birthday = birthday;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", birthday=" + birthday +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public LocalDate getBirthday() {
return birthday;
}
public void setBirthday(LocalDate birthday) {
this.birthday = birthday;
}
}
|