【问题标题】:ioexception : a required privilege is not held by client while writing in file in javaioexception:在 java 中写入文件时,客户端未持有所需的特权
【发布时间】:2012-03-28 20:36:15
【问题描述】:

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

我试图在文件中写一些东西,这给了我错误。

我的代码

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 驱动器内的文件夹)时,它工作正常。

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

【问题讨论】:

  • 您可以在 Windows 资源管理器中执行此操作吗?我假设不是因为权限问题,这可以解释为什么你也不能从 Java 中做到这一点......
  • @JigarJoshi 这需要哪种权限?如果需要,那么为什么在第二种情况下它可以在没有任何许可的情况下正常工作?
  • @assylias 谢谢。你的猜测完全正确。尝试使用 Windows 资源管理器时会导致 0x80070522 错误。能说说怎么解决吗?
  • 它适用于第二种情况,因为您对“新文件夹”有写权限(但对“C:”没有)。
  • @ChandraSekhar 我想这取决于您的 Windows 版本 - 如果您使用适当的标签 (Windows xxx) 提出问题,我相信您会得到答案...

标签: java windows file uac


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-21
    • 2012-01-03
    • 2020-10-05
    • 2011-10-23
    相关资源
    最近更新 更多