Given a directory, print all files and folders present in directory tree rooted with given directory.
We can iteratively traverse directory in BFS using below steps. We create an empty queue and we first enqueue given directory path. We run a loop while queue is not empty. We dequeue an item from queue. If the popped item is a directory, get list of all files and directories present in it, add each directory to the queue. If the popped item is a file, we print its name.
This article is contributed by Pranav. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- How to run java class file which is in different directory?
- Moving a file from one directory to another using Java
- Java program to merge contents of all the files in a directory
- Java program to List all files in a directory and nested sub-directories | Recursive approach
- Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
- Java lang.Long.highestOneBit() method in Java with Examples
- Java.util.function.BiPredicate interface in Java with Examples
- Java.util.concurrent.RecursiveTask class in Java with Examples
- Java.util.function.DoublePredicate interface in Java with Examples
- Java.util.LinkedList.peek() , peekfirst(), peeklast() in Java
- Java.util.Collections.rotate() Method in Java with Examples
- Java.util.Collections.disjoint() Method in java with Examples
- Java lang.Long.byteValue() method in Java with Examples
- Java lang.Long.reverse() method in Java with Examples
- Java lang.Long.numberOfTrailingZeros() method in Java with Examples