NIO之Files Demo

 作者简介:大家好,我是码炫码哥,前中兴通讯、美团架构师,现任某互联网公司CTO,兼职码炫课堂主讲源码系列专题


代表作:《jdk源码&多线程&高并发》,《深入tomcat源码解析》,《深入netty源码解析》,《深入dubbo源码解析》,《深入springboot源码解析》,《深入spring源码解析》,《深入redis源码解析》等


联系qq:184480602,加我进群,大家一起学习,一起进步,一起对抗互联网寒冬。码炫课堂的个人空间-码炫码哥个人主页-面试,源码等

一、遍历目录文件

    import java.io.IOException;
    import java.nio.file.*;
    import java.nio.file.attribute.BasicFileAttributes;
    import java.util.concurrent.atomic.AtomicInteger;
    
    /**
     * @author mx
     * @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 mx
     * @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 mx
     * @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 mx
     * @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);
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值