List all Java file recursively with SimpleFileVisitor
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;
public class Test {
public static void main(String[] args) throws IOException {
Path startingDir = Paths.get("/Users/java");
Files.walkFileTree(startingDir, new FindJavaVisitor());
}
}
class FindJavaVisitor extends SimpleFileVisitor<Path> {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
if (file.toString().endsWith(".java")) {
System.out.println(file.getFileName());
}
return FileVisitResult.CONTINUE;
}
}
Related examples in the same category