import java.io.*;
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private int age;
public User(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public static void main(String[] args) throws IOException, ClassNotFoundException {
// User user = new User("gol",22);
// ByteArrayOutputStream bo = new ByteArrayOutputStream();
// ObjectOutputStream oo = new ObjectOutputStream(bo);
// oo.writeObject(user);//序列化.user写入字节数组流中
// ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
// ObjectInputStream oi = new ObjectInputStream(bi);
// User userSer = (User) oi.readObject();//反序列化
// System.out.println(userSer);
User user = new User("gol",22);
FileOutputStream fos = new FileOutputStream("a.txt");
ObjectOutputStream oo = new ObjectOutputStream(fos);
oo.writeObject(user);//序列化.user写入文件中
FileInputStream fis = new FileInputStream("a.txt");
ObjectInputStream oi = new ObjectInputStream(fis);
User userSer = (User) oi.readObject();//反序列化
System.out.println(userSer);
oi.close();
fis.close();
oo.close();
fos.close();
}
}