问题描述
File file = new File(path);
if (!file.delete())
{
throw new IOException(
"Failed to delete the file because: " +
getReasonForFileDeletionFailureInPlainEnglish(file));
}
有没有一个很好的实现 getReasonForFileDeletionFailureInPlainEnglish(file)
已经在那里了否则我只需要自己写。
Is there a good implementation of getReasonForFileDeletionFailureInPlainEnglish(file)
already out there? Or else I'll just have to write it myself.
推荐答案
在Java 6中,不幸的是没有办法确定为什么文件无法删除。使用Java 7,可以使用 java.nio.file.Path#delete()
,如果文件或目录不能请删除。
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.
请注意,file.list()可能会返回可以删除的目录条目。用于删除的API文档说,只有空目录可以被删除,但是目录被认为是空的,如果包含的文件是例如操作系统的特定元数据
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.
这篇关于如何知道Java中文件删除失败的原因?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!