使用FileOutputStream时访问被拒绝 [英] Access is denied when using FileOutputStream

查看:1604
本文介绍了使用FileOutputStream时访问被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使它工作时遇到问题.它接收一个字符串,其中包含几条信息. 但是,当我尝试将String写入文件以跟踪程序随时间的变化时,我收到拒绝访问错误:

I'm having an issue getting this to work. It takes in a string which consists of several pieces of information put together. However, when I try to write the String to a file in order to track changes in the program over time, I receive an access is denied error:

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

推荐答案

您必须首先创建文件夹.但是您不能调用file.mkdirs()-您需要调用file.getParentFile().mkdirs()-否则,您将使用文件名创建一个文件夹(这将阻止您使用以下命令创建文件:相同的名字.)

You have to have folders created first. But you can't call file.mkdirs() - you need to call file.getParentFile().mkdirs() - otherwise, you will create a folder with the name of the file (which will then prevent you from creating a file with the same name).

我还要提到,您应该检查mkdirs()的结果代码,以防万一它失败.

I'll also mention that you should check the result code of mkdirs(), just in case it fails.

尽管您没有要求它,但我仍会提到您不需要调用createNewFile()(您的FileWriter会创建它).

And though you didn't ask for it, I'll still mention that you don't need to call createNewFile() (your FileWriter will create it).

为了完整起见,请确保将您的file.close()放在finally块中,并抛出异常(不要只是打印它)-在这里,您可以进行以下操作:

and, just for thoroughness, be sure to put your file.close() in a finally block, and throw your exception (don't just print it) - here you go:

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

这篇关于使用FileOutputStream时访问被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆