Netty——Files类的walkFileTree方法删除多级目录

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

一、walkFileTree方法删除多级目录

  • 代码示例
  1. import java.io.IOException;
  2. import java.nio.file.*;
  3. import java.nio.file.attribute.BasicFileAttributes;
  4. import java.util.concurrent.atomic.AtomicInteger;
  5. /**
  6. * @description:
  7. * @author: xz
  8. * @create: 2022-07-31 10:58
  9. */
  10. public class TestWalkFileTree {
  11. public static void main(String[] args) throws IOException {
  12. deleteMoreDirectory();
  13. }
  14. /**
  15. * 删除多级目录
  16. * */
  17. private static void deleteMoreDirectory() throws IOException {
  18. Files.walkFileTree(Paths.get("E:\\apache-tomcat-8.5.78-副本"), new SimpleFileVisitor<Path>() {
  19. //进入文件夹之前
  20. @Override
  21. public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
  22. System.out.println("进入文件夹===>"+dir);
  23. return super.preVisitDirectory(dir, attrs);
  24. }
  25. //遍历文件
  26. @Override
  27. public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
  28. Files.delete(file);
  29. return super.visitFile(file, attrs);
  30. }
  31. //进入文件夹之后
  32. @Override
  33. public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
  34. System.out.println("退出文件夹===>"+dir);
  35. Files.delete(dir);
  36. return super.postVisitDirectory(dir, exc);
  37. }
  38. });
  39. }
  40. }
  • 输出结果

相关文章