views:

387

answers:

4
File file = new File(path);
if (!file.delete())
{
    throw new IOException(
        "Failed to delete the file because: " +
        getReasonForFileDeletionFailureInPlainEnglish(file));
}

Is there a good implementation of getReasonForFileDeletionFailureInPlainEnglish(file) already out there? Or else I'll just have to write it myself.

A: 

As pointed out in File.delete()

you may use a SecurityManager that throws the exeception for you.

bassfriend
A: 

A deletion may fail due to one or more reasons:

  1. File does not exist (use File#exists() to test).
  2. File is locked (because it is opened by another app (or your own code!).
  3. You are not authorized (but that would have thrown a SecurityException, not returned false!).

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 + ".");
}
BalusC
@BalusC, remember that file.exists() can also throw a SecurityException.
Bob Cross
You will not get a SecurityException if the delete fails because of file system permissions.
Thilo
You will only get SecurityException if your JVM is configured restrictively, for example if you are an applet. A "normal" application would not be sandboxed here.
Thilo
@Bob: Then just wrap it all in a try/catch block on `SecurityException`. @Thilo: I didn't said anything about file system permissions. Just about an authorization failure at any level.
BalusC
+7  A: 

Hmm, best I could do:

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.";
    }
}
Cory Petosky
@Cory, file.exists(), isDirectory() and list() can all throw SecurityExcepions.
Bob Cross
@Bob: That only happens in a sandbox. And the original delete() would most likely also have thrown a SecurityException. But for completeness, I suppose he should catch it (and return "sandboxed: no file system access")
Thilo
@Thilo added, but yeah, I was addressing the question asked, not every other possibility when engaging in file I/O. :)
Cory Petosky
+4  A: 

In Java 6, there is unfortunately no way to determine why a file cannot be deleted. With Java 7, you can use java.nio.file.Path#delete() instead, which will give you a detailed cause of the failure, if the file or directory cannot be deleted.

Note that file.list() may return entries for directories, which can be deleted. The API documentation for delete says that only empty directories can be deleted, but a directory is considered empty, if the contained files are e.g. OS specific metadata files.

jarnbjo