强烈推荐一个大神的人工智能的教程:http://www.captainbed.net/zhanghan
1.Serializable
要序列化的对象,实现该接口,无需重写任何方法
2.Externalizable
要序列化的对象实现该接口,并且需要实现writeExternal和readExternal方法
- 创建一个Person类
public class Person implements Serializable{
private String id;
public Person(String id) {
System.out.println("youcacn");
this.id = id;
}
@Override
public String toString() {
return "Person{" +
"id='" + id + '\'' +
'}';
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
注意:1.Person类实现了Serializable接口
2.该实体类没有提供无参构造函数
try {
//序列化
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("2.txt"));
Person person = new Person("111");
objectOutputStream.writeObject(person);
//反序列化
ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("2.txt"));
Person person1 = (Person) objectInputStream.readObject();
System.out.println(person1.toString());
} catch (Exception e) {
e.printStackTrace();
}
结论:简单类型(无继承关系)对象序列化时必须实现序列化接口,有无无参构造函数都key
public class Student extends Person implements Serializable{
private int no;
@Override
public String toString() {
return "Student{" +
"no=" + no +
'}';
}
public Student(String id ,int no) {
super(id);
this.no = no;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
}
public class Person{
private String id;
// public Person() {
// }
public Person(String id) {
System.out.println("youcacn");
this.id = id;
}
@Override
public String toString() {
return "Person{" +
"id='" + id + '\'' +
'}';
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
public class Person implements Serializable{
private String id;
// public Person() {
// }
public Person(String id) {
System.out.println("youcacn");
this.id = id;
}
@Override
public String toString() {
return "Person{" +
"id='" + id + '\'' +
'}';
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
public class Person {
private String id;
public Person() {
}
public Person(String id) {
System.out.println("youcacn");
this.id = id;
}
@Override
public String toString() {
return "Person{" +
"id='" + id + '\'' +
'}';
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
结论:存在继承关系时,若想序列化子类对象,一并序列化父类对象,则负责类型必须实现序列化接口或者提供无参构造函数
public class Student extends Person implements Serializable {
private int no;
private Person person;
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
@Override
public String toString() {
return "Student{" +
"no=" + no +
'}';
}
public Student(String id, int no) {
super(id);
this.no = no;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
}
try {
//序列化
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("2.txt"));
Person person = new Person("111");
Student stuB = new Student("B", 163);
stuB.setPerson(person);
objectOutputStream.writeObject(stuB);
//反序列化
ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("2.txt"));
Student student = (Student) objectInputStream.readObject();
System.out.println(student.getPerson());
} catch (Exception e) {
e.printStackTrace();
}