File file = new File(path);
if (!file.delete())
{
throw new IOException(
"Failed to delete the file because: " +
getReasonForFileDeletionFailureInPlainEnglish(file));
}
已经有很好的执行getReasonForFileDeletionFailureInPlainEnglish(file)
了吗?否则我只需要自己写。如何判断文件删除在Java中失败的原因?
@BalusC,记得file.exists()还可以抛出SecurityException。 –
如果由于文件系统权限而导致删除失败,则不会得到SecurityException。 – Thilo
如果您的JVM配置受限,则只会得到SecurityException,例如,如果您是小程序。一个“正常”的应用程序不会在这里被沙箱化。 – Thilo