【问题标题】:Access is denied when using FileOutputStream使用 FileOutputStream 时访问被拒绝
【发布时间】:2013-09-02 23:56:37
【问题描述】:

我在让它工作时遇到了问题。它接收一个字符串,该字符串由几条信息放在一起。 但是,当我尝试将字符串写入文件以跟踪程序随时间的变化时,我收到访问被拒绝错误:

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

【问题讨论】:

  • 您运行该程序的用户是否有权创建文件并将其写入该目录?
  • 这是我自己的帐户,是我计算机上的管理员。我为自己和计算机上的另一位用户关闭了 UAC。
  • 不加目录直接把文件放到C盘里面就可以成功创建文件了:
  • 目录是否存在?您是否具有创建目录和写入文件的写入权限?
  • C: 盘上是否已经存在 WeatherExports 文件夹?

标签: java jsoup filenotfoundexception access-denied bufferedwriter


【解决方案1】:

您必须先创建文件夹。但是你不能调用 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();
            } 
    }

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 不错!很好的答案!节省了我很多时间。
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2023-04-10
      • 2010-12-08
      • 2011-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-14
      • 1970-01-01
      相关资源
      最近更新 更多