How to tell why a file deletion fails in Java?

前端 未结 6 1208
忘掉有多难
忘掉有多难 2020-11-27 21:13
File file = new File(path);
if (!file.delete())
{
    throw new IOException(
        \"Failed to delete the file because: \" +
        getReasonForFileDeletionFailur         


        
6条回答
  •  失恋的感觉
    2020-11-27 22:02

    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.

提交回复
热议问题