Delete Directory Recursively

Posted primerplus

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Delete Directory Recursively相关的知识,希望对你有一定的参考价值。

How to delete a directory recursively with all its subdirectories and files in Java

技术图片

In this short article, you’ll learn how to delete a directory recursively along with all its subdirectories and files.

There are two examples that demonstrate how to achieve this task. The idea behind both of the examples is to traverse the file tree, and delete the files in any directory before deleting the directory itself.

Delete directory recursively - Java 8+

This example makes use of Files.walk(Path) method that returns a Stream<Path> populated with Path objects by walking the file-tree in depth-first order.

package com.baeldung;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Comparator;

public class DeleteDirectoryRecursively {

    public static void main(String[] args) throws IOException {
        
        Path dir = Paths.get("java");

        // Traverse the file tree in depth-first fashion and delete each file/directory
        Files.walk(dir)
                .sorted(Comparator.reverseOrder())
                .forEach(path -> {
                    try {
                        System.out.println("Deleting : " + path);
                        Files.delete(path);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                });
    }
}

Delete directory recursively - Java 7

The following example uses Files.walkFileTree(Path, FileVisitor) method that traverses a file tree and invokes the supplied FileVisitor for each file.

We use a SimpleFileVisitor to perform the delete operation.

package com.zetcode;

import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;

public class DeleteDirectoryRecursively {

    public static void main(String[] args) throws IOException {
        Path dir = Paths.get("java");

        // Traverse the file tree and delete each file/directory.
        Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                System.out.println("Deleting file: " + file);
                Files.delete(file);
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                System.out.println("Deleting dir: " + dir);
                if (exc == null) {
                    Files.delete(dir);
                    return FileVisitResult.CONTINUE;
                } else {
                    throw exc;
                }
            }
        });
    }
}

以上是关于Delete Directory Recursively的主要内容,如果未能解决你的问题,请参考以下文章

为啥我使用 directory.delete 时 gif 文件不会删除?

无法使用 Directory.Delete(path, true) 删除目录

Delete Directory Recursively

Unable to delete file/directory

Unable to delete file/directory

为啥 System.IO.Directory.Delete(string,bool) 仅在 asp.net 站点模拟用户时将文件夹标记为已删除