java.io.ObjectInputStream [1]

私はインスタンスのバイナリファイルを読み込みます。

メソッド

  • readObject - 指定オブジェクトをObjectOutputStreamに読み込む

import java.io.*;
public class Main {
    public static void main(String[] args) throws Exception {
        Hero her = new Hero("yoosuke", 77, 18);
        //保存
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("rpgsave.dat");
        oos.writeObject(her);
        oos.close();

        //復元
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("rpgsave.dat");
        Hero her2 = (Hero) ois.readObject();
        ois.close();
    }
}