java.io.File delete() failing in Windows 7 (java newbie)

允我心安 提交于 2019-12-26 05:18:25

问题


I'm new to Java, just trying to make a simple utility to move, copy & delete some wav files on my pc, but java.io.File delete() fails. The wav files in question have read-only unchecked (in windows explorer) but File canWrite() returns false & setWritable(true) fails. I must be doing something stupid because nobody seems to have had this problem before?


回答1:


You are using a relative path and you are not in the directory you think you are.

Specify absolute paths or determine the current path before starting.




回答2:


Run the java application as administrator. Then try.

file.setWritable(true);
file.delete();



回答3:


Try to run the garbage collector:

File file = new File("test-file.txt");
System.gc()
boolean success = file.delete();

http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/System.html



来源:https://stackoverflow.com/questions/11056856/java-io-file-delete-failing-in-windows-7-java-newbie

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