ioexception:在用Java编写文件时,客户端不拥有必需的特权 [英] ioexception : a required privilege is not held by client while writing in file in java
问题描述
我搜索了许多类似的问题,但无法解决我的问题.
I searched many of the similar questions, but could not solve my problem.
我正在尝试在文件中写一些东西,这给了我错误.
I am trying to write something in a file, which gives me error.
我的代码
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驱动器中的文件夹)时,则工作正常.
This error arises only when file_name
is C:\\Test.txt
but when I change this file_name
value to C:\\New Folder\\Test.txt
(where New Folder
is a folder inside C Drive), then it works fine.
为什么我们不能在C Drive中创建文件?
Why we are not able to create a file inside C Drive?
推荐答案
从Windows Vista开始,默认的Windows设置不允许用户使用标准特权在C:驱动器的根目录中创建文件.如果需要在磁盘根目录中创建文件,则需要管理员权限并以管理员身份运行该应用程序(或通过其他方式提升为管理员特权).
Since Windows Vista onwards, the default Windows set up does not allow users to create files in the root of the C: drive with standard privileges. If you need to create a file in a the root of a disk, you need admin rights and to run the app as administrator (or elevate to admin privileges some other way).
这篇关于ioexception:在用Java编写文件时,客户端不拥有必需的特权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!