Java: Traversing directory in Java using BFS

Level : Beginner
Mentor: Shailendra Chauhan
Type : GuidedLab
Points : 10
Duration : 00:20:00

Lab Details

Problem Statement:

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. 

Self-paced Membership
  • 22+ Video Courses
  • 800+ Hands-On Labs
  • 400+ Quick Notes
  • 55+ Skill Tests
  • 45+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
CONTACT US
Accept cookies & close this