A deletion may fail due to one or more reasons:
- File does not exist (use File#exists() to test).
- File is locked
(because it is opened by another app (or your own code!).
- You are not
authorized (but that would have thrown a SecurityException, not
returned false!).
This function could help:
public String getReasonForFileDeletionFailureInPlainEnglish(File file) {
try {
if (!file.exists())
return "It doesn't exist in the first place.";
else if (file.isDirectory() && file.list().length > 0)
return "It's a directory and it's not empty.";
else
return "Somebody else has it open, we don't have write permissions, or somebody stole my disk.";
} catch (SecurityException e) {
return "We're sandboxed and don't have filesystem access.";
}
}
How to tell why a file deletion fails in Java?