首先创建Student类 getter/setter toString 构造方法
/**
* @author DouZi
* @Blog areone.top
*/
public class Student implements Serializable {
private String name;
private String lv;
private int id;
private String other;
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", lv='" + lv + '\'' +
", id=" + id +
", other='" + other + '\'' +
'}';
}
public Student(String name, String lv, int id, String other) {
this.name = name;
this.lv = lv;
this.id = id;
this.other = other;
}
public Student() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLv() {
return lv;
}
public void setLv(String lv) {
this.lv = lv;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getOther() {
return other;
}
public void setOther(String other) {
this.other = other;
}
}
对象流测试:
对象输出流:
OutputStream out = new FileOutputStream("E:/AAA/b/ab.txt");
ObjectOutputStream oos = new ObjectOutputStream(out);
Student st = new Student("DouZi","高级",01,"很酷哦~");
oos.writeObject(st);
oos.close();
//需要Student类 SerializableException 序列化接口
对象输入流(反序列化) :
@Test
//读档
public void du01() throws Exception{
InputStream is = new FileInputStream("E:/AAA/b/ab.txt");
ObjectInput ois = new ObjectInputStream(is);
Object o = ois.readObject();
System.out.println(o);
ois.close();
}