Traverse a directory and list all files in Java
This post will discuss how to traverse a directory, and list all files present in it and all its subdirectories in Java 7 and 8.
In the previous post, we have seen how to traverse the given directory, and print all files present in it and its subdirectories using BFS and DFS. This post will discuss the latest API introduced in Java 7 and Java 8 for this purpose.
1. Using Java 8
In Java 8, we can use the Files.walk(…)
method that takes the starting file as a parameter and return a Stream
of Path
objects by walking the file tree in a depth-first manner, which is rooted at a given starting file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; class Main { // Method to traverse a directory and list all the files in Java 8 and above public static void main(String[] args) { // Root directory String rootDir = "/var/www/html"; try { // using `Files.walk()` method Files.walk(Paths.get(rootDir)) .filter(Files::isRegularFile) .forEach(System.out::println); } catch (IOException e) { e.printStackTrace(); } } } |
We can use the try-with-resources construct to ensure that the stream is closed after the stream operations are completed. The following example uses the Files.walk(…)
method with try-with-resources.
1 2 3 4 5 6 7 8 9 10 |
try { // using `Files.walk()` method with try-with-resources try (Stream<Path> paths = Files.walk(Paths.get(rootDir))) { paths.filter(Files::isRegularFile) .forEach(System.out::println); } } catch (IOException e) { e.printStackTrace(); } |
We can also use the Files.find(…)
method that walks the file tree exactly the manner specified by the walk method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.stream.Stream; class Main { // Method to traverse a directory and list all the files in Java 8 and above public static void main(String[] args) { // Root directory String rootDir = "/var/www/html"; try { // using `Files.find()` method with try-with-resources try (Stream<Path> paths = Files.find(Paths.get(rootDir), Integer.MAX_VALUE, (path, file) -> file.isRegularFile())) { paths.forEach(System.out::println); } } catch (IOException e) { e.printStackTrace(); } } } |
2. Using Java 7
In Java 7, we can use Files.walkFileTree(…)
that walks a file tree. It requires only a starting point and an instance of FileVisitor
to invoke for each file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import java.io.IOException; import java.nio.file.*; import java.nio.file.attribute.BasicFileAttributes; class Main { // Method to traverse a directory and list all the files in Java 7 public static void main(String[] args) { // Root directory String rootDir = "/var/www/html"; try { // using `Files.walkFileTree()` method in Java 7 Files.walkFileTree(Paths.get(rootDir), new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path filePath, BasicFileAttributes attributes) { System.out.println(filePath); return FileVisitResult.CONTINUE; } }); } catch (IOException e) { e.printStackTrace(); } } } |
That’s all about traversing a directory and listing all files in Java.
Also see:
List all files in a directory in Java using Guava and Apache Commons IO
Reference: https://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)