Netty——Files类的walkFileTree方法遍历文件夹下jar包的数量

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

一、walkFileTree方法遍历文件夹下jar包的数量

  • 示例代码
  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 10:48
  10. */
  11. public class TestWalkFileTree {
  12. public static void main(String[] args) throws IOException {
  13. foreachJar();
  14. }
  15. /**
  16. * 遍历文件夹下jar包的数量
  17. * */
  18. private static void foreachJar() throws IOException {
  19. //计数器:jar包数量
  20. AtomicInteger jarCount = new AtomicInteger();
  21. //walkFileTree遍历文件树
  22. Files.walkFileTree(Paths.get("D:\\Java\\jdk1.8.0_161"),new SimpleFileVisitor<Path>(){
  23. //重写遍历文件方法
  24. @Override
  25. public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
  26. if(file.toString().endsWith(".jar")){
  27. System.out.println(file);
  28. jarCount.incrementAndGet();
  29. }
  30. return super.visitFile(file, attrs);
  31. }
  32. });
  33. System.out.println("jar count:" +jarCount);
  34. }
  35. }
  • 输出结果如下:

相关文章