IO流:序列化的示例
对象流
package com.xuedao.pojo;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
/**
* 序列化的文件都以ser为后缀名
* @author 阿超
*
*/
public class MainClass {
public static void main(String[] args) throws IOException {
User user = new User(1,"陈世美","男");
File f = new File("User.ser");
ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream(f));
o.writeObject(user);
o.close();
}
}
package com.xuedao.pojo;
import java.io.Serializable;//标识接口,作用就是做一个标记
/**
* 序列化使用
* @author 阿超
*
*/
public class User implements Serializable{
private int id;
private String sex;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public User(int id, String sex, String name) {
this();
this.id = id;
this.sex = sex;
this.name = name;
}
public User() {
super();
System.out.println("。。。。。。。。。。。。");
}
@Override
public String toString() {
return "User [id=" + id + ", sex=" + sex + ", name=" + name + "]";
}
}