How to tell why a file deletion fails in Java?
Asked Answered
S

6

42
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.

Sprinkle answered 13/11, 2009 at 12:50 Comment(0)
G
28

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.Files#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.

Geraud answered 13/11, 2009 at 13:9 Comment(2)
This delete method does not seem to exist in Java 7 API. link Edit: just found it it is now in the Files class. linkTasker
Does it throw when a file fails to be deleted ? Return type is void ! Its docs are unclear. Asked here : #19936124Sakti
C
22

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.";
    }
}
Carillo answered 13/11, 2009 at 13:2 Comment(3)
@Cory, file.exists(), isDirectory() and list() can all throw SecurityExcepions.Casiano
@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")Lunula
@Lunula added, but yeah, I was addressing the question asked, not every other possibility when engaging in file I/O. :)Carillo
M
10

Be aware that it can be your own application that prevents a file from being deleted!

If you wrote to the file previously and didn't close the writer, you are locking the file yourself.

Mourant answered 5/7, 2012 at 8:53 Comment(1)
When testing on Windows 7 using Java 6 I had this problem with a reader as well. I tried deleting the file before closing the reader and it failed.Spender
H
10

Java 7 java.nio.file.Files class can be also used:

http://docs.oracle.com/javase/tutorial/essential/io/delete.html

try {
    Files.delete(path);
} catch (NoSuchFileException x) {
    System.err.format("%s: no such" + " file or directory%n", path);
} catch (DirectoryNotEmptyException x) {
    System.err.format("%s not empty%n", path);
} catch (IOException x) {
    // File permission problems are caught here.
    System.err.println(x);
}
Hafner answered 19/2, 2015 at 10:11 Comment(0)
S
5

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 + ".");
}
Shuttlecock answered 13/11, 2009 at 12:59 Comment(4)
@BalusC, remember that file.exists() can also throw a SecurityException.Casiano
You will not get a SecurityException if the delete fails because of file system permissions.Lunula
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.Lunula
@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.Shuttlecock
E
-1

As pointed out in File.delete()

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

Edmondson answered 13/11, 2009 at 12:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.