public static void main(String[] args) throws Exception {
writeObject();
readObject();
}
/**
* 反序列化
*/
private static void readObject() throws IOException, ClassNotFoundException {
ObjectInputStream ois=new ObjectInputStream(new FileInputStream("11.txt"));
Object obj = ois.readObject();
Tetcher s= (Tetcher) obj;
System.out.println(s.getName()+","+s.getAge());
ois.close();
}
/**
* 对象序列化
*/
private static void writeObject() throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("11.txt"));
Tetcher s = new Tetcher("张三", 30);
oos.writeObject(s);
oos.close();
}
import lombok.Data;
import java.io.Serializable;
@Data
public class Tetcher implements Serializable {
private static final long serialVersionUID = 42L;
private String name;
private transient int age;
public Tetcher(String name, int age) {
this.name = name;
this.age = age;
}
}