public class Person implements Serializable {
private String name;
private int age;
private GregorianCalendar birthday;
public Person(){
}
public Person(String name, int age, GregorianCalendar birthday){
this.name = name;
this.age = age;
this.birthday = birthday;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public GregorianCalendar getBirthday() {
return birthday;
}
public void setBirthday(GregorianCalendar birthday) {
this.birthday = birthday;
}
public static void main(String[] args) {
Person p = new Person("张三",13,new GregorianCalendar(1985,8,1));
Person p2 = null;
try{
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(byteOut);
oos.writeObject(p);
ByteArrayInputStream bis = new ByteArrayInputStream(byteOut.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bis);
p2 = (Person)ois.readObject();
} catch(IOException e){
e.printStackTrace();
} catch(ClassNotFoundException e){
e.printStackTrace();
}
System.out.print(p2.getName());
}
}
程序的输出结果为“张三”