import java.io.*;
@SuppressWarnings("serial")
class Person implements Serializable {
// 不让反序列化 ,该值为类型的默认值
private String name;
private transient int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person[ name = " + name + ",age=" + age + "]";
}
}
class SerializableTest {
public static void main(String[] args) throws Exception {
ser(new Person("小张", 18));
dser();
}
public static void ser(Person person) throws Exception {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("d:" + File.separator + "1.txt"));
oos.writeObject(person);
oos.close();
}
public static void dser() throws Exception {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("d:" + File.separator + "1.txt"));
Object obj = ois.readObject();
System.out.println(obj);
ois.close();
}
}