我在java中遇到了一些問題。我創建了一個新的文件 而該文件是在一個名爲mainPlayerVariables
類及其變量是public static File savePlayerCoins = new File("c:\\Games\\Coin_Clicker\\saves\\pc.txt");
但在mainGame
腳本,我的JFrame是我添加了一個JButton,並在其上這個的onClick功能:創建新文件時發生Java錯誤
mainPlayerVariables.playerCoins++;
lblCoins.setText("x" + mainPlayerVariables.playerCoins);
try {
BufferedWriter bwSaveCoins = new BufferedWriter(new FileWriter(mainPlayerVariables.savePlayerCoins));
bwSaveCoins.write(mainPlayerVariables.playerCoins);
bwSaveCoins.close();
mainPlayerVariables.savePlayerCoins.getParentFile().mkdirs();
mainPlayerVariables.savePlayerCoins.createNewFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
現在我不知道爲什麼Java向bufferedWriter顯示錯誤並創建新的文件代碼。
請幫助,如果你知道。謝謝。 以下是錯誤:
java.io.FileNotFoundException: c:\Games\Coin_Clicker\saves\pc.txt (The system cannot find the path specified)
at java.io.FileOutputStream.open0(Native Method)
at java.io.FileOutputStream.open(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileWriter.<init>(Unknown Source)
at mainGame.mainGamePlay$2.mouseReleased(mainGamePlay.java:82)
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$500(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$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.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$JavaSecurityAccessImpl.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)
如果我在文件選項錯誤,請幫我改正。 :)
上面清清楚楚地寫着FileNotFoundException異常吧? 「系統找不到指定的路徑」 – developer
是的,但是爲什麼。我有代碼getParentFile()。mkdirs(); –
你的代碼沒有任何意義。你應該調用'mkdirs()'* first *',*然後*'new FileWriter()',並且寫入和關閉,而不是調用'createNewFile()'* * – EJP