Be the first user to complete this post
|
Add to List |
495. Print all nested directories and files in a given directory - Recursion
Given a source directory, write a recursive program to print all the nested directories and files in the source directory.
Example:
[Hellojava] [target] original-Hellojava-1.0-SNAPSHOT.jar [classes] [com] [example] [lambda] [demo] Hello.class [maven-archiver] pom.properties Hellojava-1.0-SNAPSHOT.jar pom.xml Hellojava.iml [src] [test] [java] [main] [resources] [java] [com] Hello.java
Approach:
- Check if given file is directory,
- if yes then print its name.
- Iterate through all the files inside the directory
- If the current file is a directory then make a recursive call for directory
- If the current file is not directory then print the file name.
- Else print the file name.
- See the code below for better understanding.
NOTE: with every recursive call, add tab space for better printing of hierarchy of files.
Output:
[Hellojava] [target] original-Hellojava-1.0-SNAPSHOT.jar [classes] [com] [example] [lambda] [demo] Hello.class [maven-archiver] pom.properties Hellojava-1.0-SNAPSHOT.jar pom.xml Hellojava.iml [src] [test] [java] [main] [resources] [java] [com] Hello.java