我搜索了許多類似的問題,但無法解決我的問題。ioexception:在java文件中寫入時,客戶不需要特權
我想寫一個文件中的東西,這給了我錯誤。
我的代碼
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_name
是C:\\Test.txt
但該錯誤發生時,我此file_name
值更改爲C:\\New Folder\\Test.txt
(其中New Folder
爲C驅動器內的文件夾) ,那麼它工作正常。
爲什麼我們無法在C驅動器內創建文件?
你能做到這一點在Windows資源管理器?我會假設不是因爲權限問題,這將解釋爲什麼你不能從Java做到這一點...... – assylias 2012-03-12 12:41:46
@JigarJoshi這種權限需要哪種權限?如果需要,那麼爲什麼在第二種情況下它沒有任何許可就能正常工作? – 2012-03-12 12:43:35
@assylias謝謝。你的猜測是絕對正確的。它嘗試使用Windows資源管理器時會導致0x80070522錯誤。你能說出如何解決它嗎? – 2012-03-12 12:46:43