如果希望从二进制流中恢复对象,则可以进行反序列化。

public class ReadObject {
    public static void main(String[] args) {
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
                "test.txt"))) {
            Person p = (Person) ois.readObject();
            System.out
                    .println("p.name=" + p.getName() + ",p.age=" + p.getAge());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}