我搜索了许多类似的问题,但无法解决我的问题。ioexception:在java文件中写入时,客户不需要特权
我想写一个文件中的东西,这给了我错误。
我的代码
try {
File f = new File(file_name);
f.createNewFile();
//System.out.println("Hello");
f.setWritable(true);
FileWriter fstream = new FileWriter(f);
BufferedWriter out = new BufferedWriter(fstream);
ListIterator<String> itr = account.listIterator();//account is a List object
while (itr.hasNext()) {
String element = itr.next();
out.write(element);
out.newLine();
}
out.close();
} catch (IOException e) {
e.printStackTrace();
}
该错误是
java.io.IOException: A required privilege is not held by the client
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(Unknown Source)
at com.example.Test.main(Test.java:25)
只有当file_name
是C:\\Test.txt
但该错误发生时,我此file_name
值更改为C:\\New Folder\\Test.txt
(其中New Folder
为C驱动器内的文件夹) ,那么它工作正常。
为什么我们无法在C驱动器内创建文件?
你能做到这一点在Windows资源管理器?我会假设不是因为权限问题,这将解释为什么你不能从Java做到这一点...... – assylias 2012-03-12 12:41:46
@JigarJoshi这种权限需要哪种权限?如果需要,那么为什么在第二种情况下它没有任何许可就能正常工作? – 2012-03-12 12:43:35
@assylias谢谢。你的猜测是绝对正确的。它尝试使用Windows资源管理器时会导致0x80070522错误。你能说出如何解决它吗? – 2012-03-12 12:46:43