ioexception:客户端在java中写入文件时不保留所需的权限

Cha*_*har 2 java windows uac file

我搜索了许多类似的问题,但无法解决我的问题.

我想在文件中写一些东西,这给了我错误.

我的代码

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();
}
Run Code Online (Sandbox Code Playgroud)

错误是

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)
Run Code Online (Sandbox Code Playgroud)

此错误仅在以下file_name情况下出现,C:\\Test.txt但是当我将此file_name值更改为C:\\New Folder\\Test.txt(New FolderC Drive中的文件夹)时,它可以正常工作.

为什么我们无法在C Drive中创建文件?

rav*_*ned 6

从Windows Vista开始,默认的Windows设置不允许用户使用标准权限在C:驱动器的根目录中创建文件.如果您需要在磁盘的根目录中创建文件,则需要管理员权限并以管理员身份运行应用程序(或以其他方式提升为管理员权限).