import java.io.*;
import java.lang.String;
import java.util.Scanner;
public class Test {
public static void main(String[] args) throws IOException {
//对象输出流
Stu s1 = new Stu("张三",16);
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("d:\\object.txt"));
oos.writeObject(s1);
//对象输入流
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("d:\\object.txt"));
try { //此处必须捕获异常
Stu s2 = (Stu) ois.readObject(); //将读到的对象赋值给s2
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
System.out.println(s2.name+" "+s2.age);
}
}
//输出对象的类必须实现Serializable接口
class Stu implements Serializable {
String name;
int age;
public Stu(String name,int age) {
this.name = name;
this.age = age;
}
}