java实现序列化和反序列化
package com.shengun.demo01.pojo;
import java.io.*;
public class User implements Serializable,Cloneable {
private int id;
private String name;
public User(int id, String name) {
this.id = id;
this.name = name;
}
public static void main(String[] args) throws Exception{
User user = new User(1, "张三");
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("1.txt"));
oos.writeObject(user);
oos.close();
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("1.txt"));
Object o = ois.readObject();
ois.close();
System.out.println((User)o);
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}