使用FileOutputStream时拒绝访问

时间:2013-09-02 23:56:38

标签: java jsoup filenotfoundexception access-denied bufferedwriter

我遇到了解决这个问题的问题。它包含一个由多条信息组成的字符串。 但是,当我尝试将String写入文件以便跟踪程序随时间的变化时,我收到一个拒绝访问错误:

 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();
    }

STACKTRACE YEILDS:

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

完整Stacktrace:

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));

3 个答案:

答案 0 :(得分:11)

您必须先创建文件夹。但你不能调用file.mkdirs() - 你需要调用file.getParentFile()。mkdirs() - 否则,你将创建一个文件名的文件夹(这将阻止你创建一个文件同名)。

我还要提一下,你应该检查mkdirs()的结果代码,以防它失败。

虽然你没有要求它,但我仍然会提到你不需要调用createNewFile()(你的FileWriter会创建它)。

并且,为了彻底,请确保将file.close()放在finally块中,并抛出异常(不要只打印它) - 在这里:

 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();
            } 
    }

答案 1 :(得分:6)

还有另一种可能性(仅适用于事后可能正在阅读此内容的任何人)。我有同样的问题,但所有的父文件夹都存在。问题是,有一个文件夹与我试图创建的文件同名。

答案 2 :(得分:0)

就我而言,我正在传递目录,我应该将要生成的文件放在该目录中。 我只是将Filename附加到目录中,我的工作正常。