2016-11-26 31 views
-1

我在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) 

如果我在文件选项错误,请帮我改正。 :)

+0

上面清清楚楚地写着FileNotFoundException异常吧? “系统找不到指定的路径” – developer

+0

是的,但是为什么。我有代码getParentFile()。mkdirs(); –

+0

你的代码没有任何意义。你应该调用'mkdirs()'* first *',*然后*'new FileWriter()',并且写入和关闭,而不是调用'createNewFile()'* * – EJP

回答

1

嗯,伙计们,我发现了为什么,没有必要回答。 :)顺便谢谢所有。

我已将mainPlayerVariables.savePlayerCoins.getParentFile().mkdirs(); 代码放在错误的地方。

这是修正:

mainPlayerVariables.savePlayerCoins.getParentFile().mkdirs(); 
try { 
      BufferedWriter bwSaveCoins = new BufferedWriter(new FileWriter(mainPlayerVariables.savePlayerCoins)); 
      bwSaveCoins.write(mainPlayerVariables.playerCoins); 
      bwSaveCoins.close(); 

      mainPlayerVariables.savePlayerCoins.createNewFile(); 
     } catch (IOException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 
+0

大家知道,你的'createNewFile()'调用不会做任何事情,因为这个文件已经存在了。 – qxz

+0

我知道,谢谢。 :) –

+0

所以你应该删除它。 – EJP

相关问题