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