gpt4 book ai didi

Netty——Files类的walkFileTree方法删除多级目录

转载 作者:知者 更新时间:2024-03-12 00:23:42 25 4
gpt4 key购买 nike

一、walkFileTree方法删除多级目录

  • 代码示例
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @description:
 * @author: xz
 * @create: 2022-07-31 10:58
 */
public class TestWalkFileTree {
    public static void main(String[] args) throws IOException {
        deleteMoreDirectory();
    }
     /**
     * 删除多级目录
     * */
    private static void deleteMoreDirectory() throws IOException {
        Files.walkFileTree(Paths.get("E:\\apache-tomcat-8.5.78-副本"), new SimpleFileVisitor<Path>() {
            //进入文件夹之前
            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                System.out.println("进入文件夹===>"+dir);
                return super.preVisitDirectory(dir, attrs);
            }
            //遍历文件
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                Files.delete(file);
                return super.visitFile(file, attrs);
            }
            //进入文件夹之后
            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                System.out.println("退出文件夹===>"+dir);
                Files.delete(dir);
                return super.postVisitDirectory(dir, exc);
            }
        });
    }
}
  • 输出结果

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com