设置要访问的目录级别的最大数量,以确保遍历所有级别
// 来 自 n o w j a v a . c o m import java.io.IOException; import java.nio.file.FileVisitOption; import java.nio.file.FileVisitResult; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.SimpleFileVisitor; import java.util.EnumSet; public class Main { public static void main(String[] args) throws Exception { Path listDir = Paths.get("C:/folder1"); // define the starting file ListTree walk = new ListTree(); // instantiate the walk EnumSet<FileVisitOption> opts = EnumSet.of(FileVisitOption.FOLLOW_LINKS); // follow links try {/** 时 代 Java 公 众 号 - nowjava.com 提供 **/ Files.walkFileTree(listDir, opts, Integer.MAX_VALUE, walk); // start the // walk } catch (IOException e) { System.err.println(e); } } } class ListTree extends SimpleFileVisitor<Path> { @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) { System.out.println(