• Java文件目录相关操作


    遍历目录

    • 代码示例
        Path path = Paths.get("D:\\copycentos");
            AtomicInteger dirCount = new AtomicInteger();
            AtomicInteger fileCount = new AtomicInteger();
            Files.walkFileTree(path,new SimpleFileVisitor<Path>(){
                // 访问文件目录前....todo
                @Override
                public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                        System.out.println(dir);
                        dirCount.incrementAndGet();
                        return super.preVisitDirectory(dir, attrs);
                }
                // 访问文件.....todo
                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    System.out.println(file);
                    fileCount.incrementAndGet();
                    return super.visitFile(file, attrs);
                }
     			// 访问文件失败....todo
                @Override
                public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
                    return super.visitFileFailed(file, exc);
                }
    			// 访问目录后....todo
                @Override
                public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                    return super.postVisitDirectory(dir, exc);
                }
            });
            System.out.println(dirCount);
            System.out.println(fileCount); 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    删除多级目录

    • 示例代码
      • 会删除本机目录和内层目录
       Path path = Paths.get("D:\\ceshi2");
            Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                    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 visitFileFailed(Path file, IOException exc) throws IOException {
                    return super.visitFileFailed(file, exc);
                }
    
                @Override
                public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                    Files.delete(dir);
                    return super.postVisitDirectory(dir, exc);
                }
            });
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    拷贝多级目录

    • 示例代码
       String source = "D:\\ceshi";
            String target = "D:\\ceshi22";
            Files.walk(Paths.get(source))
                    .forEach(path -> {
                        try {
                            String targetName = path.toString().replace(source, target);
                            if (Files.isDirectory(path)) {
                                Files.createDirectories(Paths.get(targetName));
                            } else if (Files.isRegularFile(path)) {
                                Files.copy(path, Paths.get(targetName));
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    });
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
  • 相关阅读:
    数据库实验三 数据查询二
    LeetCode 阿姆斯特朗数
    1.1、计算机网络在信息时代的作用
    服务网格和微服务架构的关系:理解服务网格在微服务架构中的角色和作用
    搜索引擎排名因素有哪些具体的细节?
    编程模式-表驱动编程
    买家的诉求决定你的产品卖点
    【React Hooks原理 - useRef】
    VulnHub — CH4INRULZ_v1.0.1
    2020 ICPC 澳门(G,J,I)详解
  • 原文地址:https://blog.csdn.net/qq_35310348/article/details/127803577