基于前文对 Files.walkFileTree 递归遍历及目录删除的讨论,Files.walk 是 Java 8 引入的更简洁的流式(Stream)遍历方法。它将目录树转换为 Stream
简单示例:
packagecom.example.demo;importjava.io.IOException;importjava.nio.file.Files;importjava.nio.file.Paths;publicclassTestWalk{publicstaticvoidmain(String[]args)throwsIOException{Stringsource="C:\\Users\\TS\\Downloads\\demo";Stringtarget="C:\\Users\\TS\\Downloads\\test";Files.walk(Paths.get(source)).forEach(path->{Stringreplace=path.toString().replace(sou