Java 使用 FileOutputStream 时访问被拒绝
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18582223/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Access is denied when using FileOutputStream
提问by Nick
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();
}
STACKTRACE YEILDS:
堆栈跟踪收益:
java.io.FileNotFoundException: C:\WeatherExports\export.txt (Access is denied)
Full 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.accessBufferedWriter inFile = new BufferedWriter(new FileWriter(file,true));
0(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext.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)
Line 58:
第 58 行:
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();
}
}
采纳答案by Kevin Day
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).
您必须先创建文件夹。但是你不能调用 file.mkdirs() - 你需要调用 file.getParentFile().mkdirs() - 否则,你将创建一个带有文件名的文件夹(这将阻止你创建一个文件同名)。
I'll also mention that you should check the result code of mkdirs(), just in case it fails.
我还会提到您应该检查 mkdirs() 的结果代码,以防万一失败。
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).
虽然您没有要求它,但我仍然会提到您不需要调用 createNewFile() (您的 FileWriter 将创建它)。
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:
并且,为了彻底起见,请务必将您的 file.close() 放在 finally 块中,并抛出您的异常(不要只是打印它) - 在这里:
##代码##回答by k_g
There's another possibility (just for anyone who may be reading this after the fact). I had the same problem, but all the parent folders existed. The problem turned out to be that there was a folder with the same name as the file I was trying to create.
还有另一种可能性(仅适用于事后阅读本文的任何人)。我遇到了同样的问题,但所有父文件夹都存在。问题原来是有一个与我试图创建的文件同名的文件夹。
回答by Leroy
On my case I was passing the directory where I should put the file I am generating. I just appended the Filename to the directory and mine worked fine.
在我的情况下,我传递了应该放置我生成的文件的目录。我只是将文件名附加到目录中,我的工作正常。