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

时间:2012-03-12 12:39:40

标签: 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 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_nameC:\\Test.txt但我将此file_name值更改为C:\\New Folder\\Test.txt时,才会出现此错误(其中New Folder是C Drive中的文件夹) ,那就行了。

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

1 个答案:

答案 0 :(得分:6)

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