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

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

一、walkFileTree方法拷贝多级目录

  • 代码示例
  1. package com.example.nettytest.nio.day2;
  2. import java.io.IOException;
  3. import java.nio.file.*;
  4. import java.nio.file.attribute.BasicFileAttributes;
  5. import java.util.concurrent.atomic.AtomicInteger;
  6. /**
  7. * @description:
  8. * @author: xz
  9. * @create: 2022-07-31 11:08
  10. */
  11. public class TestWalkFileTree {
  12. public static void main(String[] args) throws IOException {
  13. copyMoreDirectory();
  14. }
  15. /**
  16. * 拷贝多级目录
  17. * */
  18. private static void copyMoreDirectory() throws IOException {
  19. long start = System.currentTimeMillis();
  20. String source = "E:\\apache-tomcat-8.5.78-副本";
  21. String target = "E:\\apache-tomcat-8.5.78-副本-666";
  22. //拷贝多级目录
  23. Files.walk(Paths.get(source)).forEach(path -> {
  24. //原路径替换成一个新的路径
  25. String targetName = path.toString().replace(source, target);
  26. //如果是目录
  27. if(Files.isDirectory(path)){
  28. try {
  29. Files.createDirectory(Paths.get(targetName));
  30. } catch (IOException e) {
  31. e.printStackTrace();
  32. }
  33. }
  34. //如果是普通文件
  35. if(Files.isRegularFile(path)){
  36. //拷贝
  37. try {
  38. Files.copy(path, Paths.get(targetName));
  39. } catch (IOException e) {
  40. e.printStackTrace();
  41. }
  42. }
  43. });
  44. long end = System.currentTimeMillis();
  45. System.out.println("计算出拷贝文件的时间差===="+(end - start));
  46. }
  47. }
  • 输出结果

相关文章