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

2019-09-20 11:51发布

问题:

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