Netty——Files类的walkFileTree方法拷贝多级目录

x33g5p2x  于2022-08-17 转载在 其他  
字(1.1k)|赞(0)|评价(0)|浏览(226)

一、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));
    }
}
  • 输出结果

相关文章

微信公众号

最新文章

更多