日期:2014-05-20 浏览次数:20935 次
public class Person_Serible implements Serializable{}
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Date;
public class SerializableDemo {
public static void main(String[] args) throws Exception {
Person pers = new Person();
pers.setName("Lee");
pers.setAge(20);
pers.setBirthday(new Date());
PersonSerializable perSeri = new PersonSerializable();
perSeri.copy(pers);
ByteArrayOutputStream bou = new ByteArrayOutputStream();
ObjectOutputStream oou = new ObjectOutputStream(bou);
oou.writeObject(perSeri);
ByteArrayInputStream bin = new ByteArrayInputStream(bou.toByteArray());
ObjectInputStream oin = new ObjectInputStream(bin);
PersonSerializable perSeriClone = (PersonSerializable)oin.readObject();
System.out.println(perSeriClone.getName());
System.out.println(perSeriClone.getAge());
System.out.println(perSeriClone.getBirthday());
System.out.println("======================");
Person persClone = perSeriClone.copyToPerson();
System.out.println(persClone.getName());
System.out.println(persClone.getAge());
System.out.println(persClone.getBirthday());
}
}
class PersonSerializable implements Serializable {
private static final long serialVersionUID = -8564152324839732089L;
private String name;
private int age;
private Date birthday;
public void copy(Person person){
if (person == null) return;
this.setName(person.getName());
this.setBirthday(person.getBirthday());
this.setAge(person.getAge());
}
public Person copyToPerson(){
Person person = new Person();
person.setName(this.getName());
person.setBirthday(this.getBirthday());
person.setAge(this.getAge());
return person;
}
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 Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
class Person {
private String name;
private int age;
private Date 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 Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
public class Person {
private String name;
private int age;
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;
}
}
import java.io.IOException;
import java.io.Serializable;
public class Person_Serible extends Person implements Serializable {
private static final long serialVersionUID = 1L;
private void writeObject(java.io.ObjectOutputStream out) throws IOException {
out.defaultWriteObject();// 先序列化对象
out.writeUTF(super.getName());// 再序列化父类的域
out.writeInt(super.getAge());// 再序列化父类的域
}
private void readObject(java.io.ObjectInputStream in) throws IOException,