使用FileOutputStream时出现“拒绝访问”的错误

8
我遇到了一个问题,需要解决。这个程序接收一个字符串,其中包含多个信息片段。但是,当我尝试将该字符串写入文件以跟踪程序的更改历史时,我收到了一个访问被拒绝的错误提示。
 void writeToFile(String input) throws Exception{
            File file = new File("C:\\WeatherExports\\export.txt");
            if(!file.exists()){
                    file.createNewFile();
            }
            BufferedWriter inFile = new BufferedWriter(new FileWriter(file,true));
            try{
                    inFile.append(input);
                    inFile.newLine();
            } catch(Exception e){
                    e.printStackTrace();
            }
            inFile.close();
    }

堆栈跟踪产生:

java.io.FileNotFoundException: C:\WeatherExports\export.txt (Access is denied)

完整的堆栈跟踪:

java.io.FileNotFoundException: C:\WeatherExports\export.txt (Access is denied)
at java.io.FileOutputStream.openAppend(Native Method)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileWriter.<init>(Unknown Source)
at org.weatheralert.InfoManipMethods.writeToFile(InfoManipMethods.java:58)
at org.weatheralert.Form.actionPerformed(Form.java:108)
at javax.swing.JTextField.fireActionPerformed(Unknown Source)
at javax.swing.JTextField.postActionEvent(Unknown Source)
at javax.swing.JTextField$NotifyAction.actionPerformed(Unknown Source)
at javax.swing.SwingUtilities.notifyAction(Unknown Source)
at javax.swing.JComponent.processKeyBinding(Unknown Source)
at javax.swing.JComponent.processKeyBindings(Unknown Source)
at javax.swing.JComponent.processKeyEvent(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.KeyboardFocusManager.redispatchEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(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$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$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)

第58行:

BufferedWriter inFile = new BufferedWriter(new FileWriter(file,true));

1
运行程序的用户是否具有在该目录中创建和写入文件的权限? - NG.
如果我不添加文件夹并将文件直接放在C盘上,我就可以成功地创建文件。 - Nick
该目录是否存在?您是否具有创建目录和写入文件的写入权限? - MadProgrammer
该目录不存在。我正在尝试创建它。最初我有: file.mkdirs(); file.createNewFile(); - Nick
很遗憾BufferedWriter在堆栈跟踪中没有显示。你有什么解释?很遗憾你提到的那一行也包含new FileWriter(),它在堆栈跟踪中是出现了的。很遗憾你在发布或评论之前没有好好看看自己的证据。 - user207421
显示剩余5条评论
3个回答

14

首先需要创建文件夹,但不能调用file.mkdirs()方法,需要调用file.getParentFile().mkdirs()方法,否则会创建一个与文件同名的文件夹(这将会防止你创建同名的文件)。

另外提醒一下,你应该检查mkdirs()方法的返回值,以防它失败。

虽然你没有要求,但我还是想提一下,你不需要调用createNewFile()方法(因为FileWriter会自动创建文件)。

最后,为了保险起见,请确保在finally块中调用file.close()方法,并抛出异常而不是仅仅打印信息:

 void writeToFile(String input) throws IOException{
            File file = new File("C:\\WeatherExports\\export.txt");
            if (!file.getParentFile().mkdirs())
                    throw new IOException("Unable to create " + file.getParentFile());
            BufferedWriter out = new BufferedWriter(new FileWriter(file,true));
            try{
                    out.append(input);
                    out.newLine();
            } finally {
                    out.close();
            } 
    }

6

还有一种可能性(仅适用于那些在此之后可能会阅读此内容的人)。我遇到了同样的问题,但所有父文件夹都存在。问题最终被证明是因为有一个与我试图创建的文件同名的文件夹。


不错!很棒的答案!省了我很多时间。 - ryvantage

0
在我的情况下,我传递了应该放置生成文件的目录。我只是将文件名附加到目录中,然后我的程序就正常工作了。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接