package wellGrounded; import java.io.IOException; 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.nio.file.attribute.BasicFileAttributes; /** * 遍历目录下所有的java文件,如果有子目录,会继续遍历。 * @author Felix * */ public class FindJavaFiles { public static void main(String[] args) throws IOException { Path startingDir = Paths.get("C:/workspace/mytest/src/main/java/com/lee/levi"); Files.walkFileTree(startingDir, new FindJavaVisitor()); } private static class FindJavaVisitor extends SimpleFileVisitor<Path> { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { if (file.toString().endsWith(".java")) { System.out.println(file.getFileName()); } return FileVisitResult.CONTINUE; } } }