ioexception:在用Java编写文件时,客户端不拥有必需的特权 [英] ioexception : a required privilege is not held by client while writing in file in java

查看:56
本文介绍了ioexception:在用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_nameC:\\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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆