How to tell why a file deletion fails in Java?

前端 未结 6 1215
忘掉有多难
忘掉有多难 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 21:53

    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);
    }
    

提交回复
热议问题