如何判断为什么在 Java 中文件删除失败?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1729049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to tell why a file deletion fails in Java?
提问by Arne Evertsson
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.
已经有一个很好的实现getReasonForFileDeletionFailureInPlainEnglish(file)
了吗?否则我就只能自己写了。
回答by ba__friend
As pointed out in File.delete()
正如File.delete()所指出的
you may use a SecurityManager that throws the exeception for you.
您可以使用为您抛出异常的 SecurityManager。
回答by BalusC
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!).
- 文件不存在(用于
File#exists()
测试)。 - 文件被锁定(因为它被另一个应用程序(或您自己的代码!)打开。
- 您未获得授权(但这会引发 SecurityException,而不是返回 false!)。
So whenever deletion fails, do a File#exists()
to check if it is caused by 1) or 2).
所以每当删除失败时,做一个File#exists()
检查它是由 1) 或 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 + ".");
}
回答by Cory Petosky
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.";
}
}
回答by jarnbjo
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.
遗憾的是,在 Java 6 中,无法确定无法删除文件的原因。使用 Java 7,您可以改用java.nio.file.Files#delete()
,如果无法删除文件或目录,它将为您提供详细的失败原因。
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.
请注意,file.list() 可能会返回可以删除的目录条目。删除的 API 文档说只能删除空目录,但如果包含的文件是操作系统特定的元数据文件,则目录被视为空目录。
回答by xastor
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.
如果您之前写入文件并且没有关闭写入器,则您自己锁定了文件。
回答by Quark
Java 7 java.nio.file.Filesclass can be also used:
也可以使用Java 7 java.nio.file.Files类:
http://docs.oracle.com/javase/tutorial/essential/io/delete.html
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);
}