您当前的位置: 首页 > 

小志的博客

暂无认证

  • 0浏览

    0关注

    1217博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Netty——文件编程(Files类的walkFileTree方法拷贝多级目录)

小志的博客 发布时间:2022-07-31 23:19:00 ,浏览量:0

一、walkFileTree方法拷贝多级目录
  • 代码示例

    package com.example.nettytest.nio.day2;
    
    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 11:08
     */
    public class TestWalkFileTree {
        public static void main(String[] args) throws IOException {
            copyMoreDirectory();
        }
         /**
         * 拷贝多级目录
         * */
        private static void copyMoreDirectory() throws IOException {
            long start = System.currentTimeMillis();
            String source = "E:\\apache-tomcat-8.5.78-副本";
            String target = "E:\\apache-tomcat-8.5.78-副本-666";
            //拷贝多级目录
            Files.walk(Paths.get(source)).forEach(path -> {
                //原路径替换成一个新的路径
                String targetName = path.toString().replace(source, target);
                //如果是目录
                if(Files.isDirectory(path)){
                    try {
                        Files.createDirectory(Paths.get(targetName));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                //如果是普通文件
                if(Files.isRegularFile(path)){
                    //拷贝
                    try {
                        Files.copy(path, Paths.get(targetName));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });
            long end = System.currentTimeMillis();
            System.out.println("计算出拷贝文件的时间差===="+(end - start));
        }
    }
    
  • 输出结果 在这里插入图片描述

    在这里插入图片描述

关注
打赏
1661269038
查看更多评论
立即登录/注册

微信扫码登录

0.0395s