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

2019-09-20 11:34发布

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?

3条回答
我命由我不由天
2楼-- · 2019-09-20 11:37

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

查看更多
The star\"
3楼-- · 2019-09-20 11:39

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.

查看更多
太酷不给撩
4楼-- · 2019-09-20 12:01

Run the java application as administrator. Then try.

file.setWritable(true);
file.delete();
查看更多
登录 后发表回答