2023-09-23
原文作者:李林超 原文地址: https://www.lilinchao.com/archives/2095.html

[TOC]

一、遍历目录文件

    import java.io.IOException;
    import java.nio.file.*;
    import java.nio.file.attribute.BasicFileAttributes;
    import java.util.concurrent.atomic.AtomicInteger;
    
    /**
     * @author lilinchao
     * @date 2022/5/30
     * @description 遍历目录,统计文件和文件夹个数
     **/
    public class FilesTraverseDirectoryDemo {
        public static void main(String[] args) throws IOException {
            Path path = Paths.get("D:\\Tools\\jdk1.8.0_201");
            AtomicInteger dirCount = new AtomicInteger();
            AtomicInteger fileCount = new AtomicInteger();
    
            Files.walkFileTree(path,new SimpleFileVisitor<Path>(){
                //preVisitDirectory()在访问任何目录前被调用
                @Override
                public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                    System.out.println(dir);
                    dirCount.incrementAndGet();
                    return super.preVisitDirectory(dir, attrs);
                }
    
                //visitFile()在访问任何文件时被调用
                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    System.out.println(file);
                    fileCount.incrementAndGet();
                    return super.visitFile(file, attrs);
                }
            });
            System.out.println(dirCount);
            System.out.println(fileCount);
        }
    }

二、统计Jar的数目

    import java.io.IOException;
    import java.nio.file.*;
    import java.nio.file.attribute.BasicFileAttributes;
    import java.util.concurrent.atomic.AtomicInteger;
    
    /**
     * @author lilinchao
     * @date 2022/5/30
     * @description 统计文件中Jar的个数
     **/
    public class FilesCountJarNumberDemo {
        public static void main(String[] args) throws IOException {
            Path path = Paths.get("D:\\Tools\\jdk1.8.0_201");
            AtomicInteger fileCount = new AtomicInteger();
    
            Files.walkFileTree(path,new SimpleFileVisitor<Path>(){
                //visitFile()在访问任何文件时被调用
                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    if(file.toFile().getName().endsWith(".jar")){
                        fileCount.incrementAndGet();
                    }
                    return super.visitFile(file, attrs);
                }
            });
            System.out.println(fileCount);
        }
    }

三、删除多级目录

    import java.io.IOException;
    import java.nio.file.*;
    import java.nio.file.attribute.BasicFileAttributes;
    
    /**
     * @author lilinchao
     * @date 2022/5/30
     * @description 删除多级目录
     **/
    public class FilesDeleteDirectoryDemo {
        public static void main(String[] args) throws IOException {
            Path path = Paths.get("D:\\libs2");
    
            Files.walkFileTree(path,new SimpleFileVisitor<Path>(){
                //visitFile()在访问任何文件时被调用
                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    Files.delete(file);
                    return super.visitFile(file, attrs);
                }
    
                //postVisitDirectory()在访问任何目录后被调用
                @Override
                public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                    Files.delete(dir);
                    return super.postVisitDirectory(dir, exc);
                }
            });
            System.out.println("删除完毕!");
        }
    }

四、拷贝多级目录

    import java.io.IOException;
    import java.nio.file.Files;
    import java.nio.file.Paths;
    
    /**
     * @author lilinchao
     * @date 2022/5/30
     * @description 拷贝多级目录
     **/
    public class FilesCopyDirectoryDemo {
        public static void main(String[] args) throws IOException {
            long start = System.currentTimeMillis();
            String source = "D:\\libs";
            String target = "D:\\libs_aaa";
    
            Files.walk(Paths.get(source)).forEach(path -> {
                try {
                    String targetName = path.toString().replace(source,target);
                    //是目录
                    if(Files.isDirectory(path)){
                        //createDirectory()方法利用Path创建一个新的目录
                        Files.createDirectory(Paths.get(targetName));
                    }else if(Files.isRegularFile(path)){ //是普通文件
                        //copy()方法将文件从一个path复制到另一个
                        Files.copy(path,Paths.get(targetName));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
            long end = System.currentTimeMillis();
            System.out.println(end - start);
        }
    }
阅读全文