Traverse a given directory using BFS and DFS in Java
This post will discuss how to traverse the given directory and list out all files present in it and all its sub-directories.
1. Using BFS (Breadth–First Search)
We can iteratively traverse the given directory, and print all files present within it and its sub-directories using Breadth–first search. Following is the complete algorithm:
- Create an empty queue of
Fileclass and enqueue the root directory. - Loop till queue becomes empty (all files and directories present inside the root directory are processed)
- Pop front
Filefrom the queue. - If the popped
Fileis a directory, get the list of all files and directories present in it, add each directory to the queue and print every file.
- Pop front
The algorithm can be implemented as follows in Java:
|
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
import java.io.File; import java.util.ArrayDeque; import java.util.Queue; class Main { // Iterative function to traverse a given directory in Java using BFS public static void listFilesIteratively(File root) { // maintain a queue to store files and directories Queue<File> queue = new ArrayDeque<>(); // add root directory to the queue queue.add(root); // loop till the queue is empty. i.e., all files and directories present // inside the root directory are processed while (!queue.isEmpty()) { // get the next file/directory from the queue File current = queue.poll(); // get the list of all files and directories in `current` File[] listOfFilesAndDirectory = current.listFiles(); // `listFiles()` returns non-null array if `current` denotes a directory if (listOfFilesAndDirectory != null) { // iterate over the list of the files and directories in // the current directory for (File file: listOfFilesAndDirectory) { // if the current file is a directory if (file.isDirectory()) { queue.add(file); } // otherwise, print it else { System.out.println(file); } } } } } public static void main(String[] args) { // root directory String dir = "/var/www/html"; File rootDir = new File(dir); listFilesIteratively(rootDir); } } |
2. Using DFS (Depth–First Search)
We can also recursively traverse the given directory and print all files present within it and its sub-directories using Depth–first search. The idea is to start with the root directory, get the list of all files and directories present in it, recursively explore each directory, and print every 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 30 31 32 33 34 35 36 37 |
import java.io.File; class Main { // Recursive function to traverse a given directory in Java using DFS public static void listFilesRecursively(File root) { // get the list of all files and directories present in the `root` File[] listOfFilesAndDirectory = root.listFiles(); // `listFiles()` returns non-null array if `root` denotes a directory if (listOfFilesAndDirectory != null) { for (File file : listOfFilesAndDirectory) { // if the file denotes a directory, recur for it if (file.isDirectory()) { listFilesRecursively(file); } // otherwise, print it else { System.out.println(file); } } } } public static void main(String args[]) { // root directory String dir = "/var/www/html"; File rootDir = new File(dir); // recursively print all files present in the root directory listFilesRecursively(rootDir); } } |
Also see:
List all files in a directory in Java using Guava and Apache Commons IO
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 :)