ioexception : a required privilege is not held by client while writing in file in java

做~自己de王妃 提交于 2019-12-13 16:06:22

问题


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.

My code

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();
}

The error is

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)

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.

Why we are not able to create a file inside C Drive?


回答1:


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).



来源:https://stackoverflow.com/questions/9667170/ioexception-a-required-privilege-is-not-held-by-client-while-writing-in-file-i

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!