我为我的游戏创建了一个保存和加载功能,然而,它拒绝加载。 (这是我第一次处理保存/恢复功能)。
似乎要保存,我的意思是“minesweepersavestate.ser”#39;文件出现在我的文件夹中,但是加载时出错,错误在于(枚举e = b.keys(); e.hasMoreElements();){line。它不会编译,因为它说'找不到sysmbol - 方法键',我导入了java.util。*。
有人能告诉我我的错误在哪里,所以我可以让这些功能正常工作,谢谢。
public void saveGame(){
GameBoard b = new GameBoard();
try {
System.out.println("Creating File/Object output stream...");
FileOutputStream fileOut = new FileOutputStream("minesweepersavestate.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
System.out.println("Writing GameBoard Object...");
out.writeObject(b);
System.out.println("Closing all output streams...\n");
out.close();
fileOut.close();
} catch(FileNotFoundException e) {
System.out.println("Class not found\n");
e.printStackTrace();
} catch (IOException e) {
System.out.println("no");
e.printStackTrace();
}
}
public void LoadBoard()
{
GameBoard b = null;
try {
System.out.println("Creating File/Object input stream...");
FileInputStream fileIn = new FileInputStream("minesweepersavestate.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
System.out.println("Loading GameBoard Object...");
b = (GameBoard)in.readObject();
System.out.println("Closing all input streams...\n");
in.close();
fileIn.close();
} catch (ClassNotFoundException e) {
System.out.println("Class not found\n");
e.printStackTrace();
} catch(FileNotFoundException e) {
System.out.println("File not found\n");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Printing out loaded elements...");
for (Enumeration e = b.keys(); e.hasMoreElements(); ) {
Object obj = e.nextElement();
System.out.println(" - Element(" + obj + ") = " + b.get(obj));
}