创建新文件时出现Java错误

时间:2016-11-26 06:58:20

标签: java bufferedwriter

我在java中有一点问题。我创建了一个新文件 并且该文件位于名为mainPlayerVariables的类中,其变量为public static File savePlayerCoins = new File("c:\\Games\\Coin_Clicker\\saves\\pc.txt");但是在我的JFrame所在的mainGame脚本中,我添加了一个JButton及其onClick函数:

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)

如果我在文件选项中出错,请帮我纠正。 :)

1 个答案:

答案 0 :(得分: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();
        }