File file = new File(path);
if (!file.delete())
{
throw new IOException(
\"Failed to delete the file because: \" +
getReasonForFileDeletionFailur
A deletion may fail due to one or more reasons:
File#exists() to test).So whenever deletion fails, do a File#exists() to check if it is caused by 1) or 2).
Summarized:
if (!file.delete()) {
String message = file.exists() ? "is in use by another app" : "does not exist";
throw new IOException("Cannot delete file, because file " + message + ".");
}