コマンドを入力して特定のアクションを実行する必要がある単純なテキストベースのゲームを作成しています。最近、進行状況を保存できる機能をゲームに追加しました。しかし、何らかの理由で、既存の保存ファイルにゲームを保存しようとするとクラッシュします。ゲームを保存するコードは次のとおりです (保存に失敗すると、予想どおり、「ゲーム データを保存しようとしてエラーが発生しました。ゲームは終了します。」と表示されます):
import java.util.Formatter;
import javax.swing.JOptionPane;
public class Gamesave {
private static Formatter gamesave;
private static Formatter firstTimeSave;
private static Formatter attackpoints;
private static Formatter defensepoints;
private static Formatter skillpoints;
private static Formatter wins;
private static Formatter loses;
private static Formatter money;
// Attackpoints, defensepoints, skillpoints, wins, loses, money
public static void openFile(){
try{
attackpoints = new Formatter("c:\\FightNight\\Saves\\"+MainClass.newProfileName+"\\"+MainClass.newProfileName+"_attackpoints.txt");
defensepoints = new Formatter("c:\\FightNight\\Saves\\"+MainClass.newProfileName+"\\"+MainClass.newProfileName+"_defensepoints.txt");
skillpoints = new Formatter("c:\\FightNight\\Saves\\"+MainClass.newProfileName+"\\"+MainClass.newProfileName+"_skillpoints.txt");
wins = new Formatter("c:\\FightNight\\Saves\\"+MainClass.newProfileName+"\\"+MainClass.newProfileName+"_wins.txt");
loses = new Formatter("c:\\FightNight\\Saves\\"+MainClass.newProfileName+"\\"+MainClass.newProfileName+"_loses.txt");
money = new Formatter("c:\\FightNight\\Saves\\"+MainClass.newProfileName+"\\"+MainClass.newProfileName+"_money.txt");
gamesave = new Formatter("c:\\FightNight\\Saves\\"+MainClass.newProfileName+"\\"+MainClass.newProfileName+".txt");
firstTimeSave = new Formatter("c:\\FightNight\\Game Data\\firstTimeSave.txt");
}catch (Exception e) {JOptionPane.showMessageDialog(null, "There was an error when trying to save game data. The game will now close."); System.exit(0);}
}
public static void addRecords(){
attackpoints.format("%s",MainClass.attackpoints);
defensepoints.format("%s",MainClass.defensepoints);
skillpoints.format("%s",MainClass.skillpoints);
wins.format("%s",MainClass.wins);
loses.format("%s",MainClass.loses);
money.format("%s",MainClass.money);
firstTimeSave.format("%b", MainClass.firstTime);
}
public void closeFile(){
attackpoints.close();
defensepoints.close();
skillpoints.close();
wins.close();
loses.close();
money.close();
gamesave.close();
firstTimeSave.close();
}
}
クラスを呼び出すコードは次のとおりです。
static class SaveAction implements ActionListener{
public void actionPerformed (ActionEvent e){
try{
Gamesave.openFile();
Gamesave.addRecords();
save.closeFile();
JOptionPane.showMessageDialog(null, "Your game has been saved.");
}catch (Exception e1) {JOptionPane.showMessageDialog(null, "Sorry, that is an invalid response.");}
}
}
別の注意点として、ゲームがコンピューターで初めて起動されると、保存ファイルとその他の必要なもののディレクトリが作成されます。助けてくれてありがとう!
スタック トレース:
java.io.FileNotFoundException: c:\FightNight\Saves\null\null_attackpoints.txt (The system cannot find the path specified)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.util.Formatter.<init>(Unknown Source)
at Gamesave.openFile(Gamesave.java:16)
at CommandLine$SaveAction.actionPerformed(CommandLine.java:93)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)