ioexception:在java中写入文件时,客户端未持有所需的权限
我搜索了许多类似的问题,但无法解决我的问题 我试图在一个文件中写一些东西,这给了我错误 我的代码ioexception:在java中写入文件时,客户端未持有所需的权限,java,windows,file,uac,Java,Windows,File,Uac,我搜索了许多类似的问题,但无法解决我的问题 我试图在一个文件中写一些东西,这给了我错误 我的代码 try { File f = new File(file_name); f.createNewFile(); //System.out.println("Hello"); f.setWritable(true); FileWriter fstream = new FileWriter(f); BufferedWriter out = new Buffe
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();
}
只有当file\u name
为C:\\Test.txt
时,才会出现此错误,但当我将此file\u name
值更改为C:\\New Folder\\Test.txt
(其中New Folder
是C驱动器内的文件夹)时,它工作正常
为什么不能在C驱动器内创建文件?自Windows Vista以后,默认Windows设置不允许用户以标准权限在C:驱动器的根目录下创建文件。如果您需要在磁盘根目录中创建文件,则需要管理员权限并以管理员身份运行应用程序(或以其他方式提升为管理员权限)。您可以在Windows资源管理器中执行此操作吗?我认为这不是因为权限问题,这也解释了为什么不能从Java中执行…@JigarJoshi这需要哪种权限?如果需要的话,为什么在第二种情况下,它在没有任何许可的情况下运行良好?@assylias谢谢。你的猜测完全正确。尝试使用windows资源管理器时,会导致0x80070522错误。你能告诉我怎么解决吗?它在第二种情况下有效,因为你有“新文件夹”的写入权限(但没有“C:”)@ChandraSekhar我想这取决于你的Windows版本-如果你用适当的标签(Windows xxx)问问题,我肯定你会得到答案。。。
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)