我创建了一个为我的奥赛罗游戏项目保存和加载对象的类。我的问题是如何获取此保存的对象并在其中显示已保存的游戏板状态?先谢谢你的回答!
public class SaveGame {
public static final String filename = "gameSave.bin";
public void saveGameFile(Serializable object){
FileOutputStream fs = null;
try{
fs = new FileOutputStream(filename);
ObjectOutputStream os = new ObjectOutputStream(fs);
os.writeObject(object);
os.flush();
os.close();
}catch(IOException e){
e.printStackTrace();
}
}
public void load(){
if(checkFileExists()){
FileInputStream fis = null;
try{
fis = new FileInputStream(filename);
ObjectInputStream ois = new ObjectInputStream(fis);
CreateBoard cb = (CreateBoard) ois.readObject();
ois.close();
System.out.println(cb.toString());
}catch(ClassNotFoundException | IOException e){
e.printStackTrace();
}
}
}
public boolean checkFileExists(){
return new File(filename).isFile(); //Searches directory to make sure file exists
}