Prerequisites: File class
Given a main directory/folder, list all the files from it, and if this directory has other nested sub-directories, list files from them. It is pretty easy to observe a simple recursion pattern in the above problem.
Algorithm :
- Create a File object for the main directory.
- Get an array of files for the main directory.
- If array[i] is a file:
- If array[i] is a directory :
- Print out directory name.
- Get array of files for current sub-directory.
- Repeat the step 3 and 4 with current sub-directory.
- Repeat the step 3 and 4 with next array[i].
Example 1:
Java
import java.io.File;
public class GFG {
static void RecursivePrint(File[] arr, int index, int level)
{
if (index == arr.length)
return ;
for ( int i = 0 ; i < level; i++)
System.out.print( "\t" );
if (arr[index].isFile())
System.out.println(arr[index].getName());
else if (arr[index].isDirectory()) {
System.out.println( "[" + arr[index].getName()
+ "]" );
RecursivePrint(arr[index].listFiles(), 0 ,
level + 1 );
}
RecursivePrint(arr, ++index, level);
}
public static void main(String[] args)
{
String maindirpath
= "C:\\Users\\Gaurav Miglani\\Desktop\\Test" ;
File maindir = new File(maindirpath);
if (maindir.exists() && maindir.isDirectory()) {
File arr[] = maindir.listFiles();
System.out.println(
"**********************************************" );
System.out.println(
"Files from main directory : " + maindir);
System.out.println(
"**********************************************" );
RecursivePrint(arr, 0 , 0 );
}
}
}
|
Output:
**********************************************
Files from main directory : C:\Users\Gaurav Miglani\Desktop\Test
**********************************************
Cormen.pdf
Extra-Items.pdf
XYZ.pdf
[Docs]
A.docx
B.doc
C.docx
ABC.pdf
JKL.pdf
[sheets]
XXX.csv
YYY.csv
results.pdf
[Resumes]
[Before2016]
Resume2015.doc
Resume2016.doc
[Before2014]
Resume2014.doc
Resume2017.doc
Resume2017.pdf
QA.doc
Testing.pdf
Example 2: Below is another recursive program. Here we use recursion only for nested sub-directories. For main directory files, we use foreach loop.
Java
import java.io.File;
public class GFG {
static void RecursivePrint(File[] arr, int level)
{
for (File f : arr) {
for ( int i = 0 ; i < level; i++)
System.out.print( "\t" );
if (f.isFile())
System.out.println(f.getName());
else if (f.isDirectory()) {
System.out.println( "[" + f.getName() + "]" );
RecursivePrint(f.listFiles(), level + 1 );
}
}
}
public static void main(String[] args)
{
String maindirpath
= "C:\\Users\\Gaurav Miglani\\Desktop\\Test" ;
File maindir = new File(maindirpath);
if (maindir.exists() && maindir.isDirectory()) {
File arr[] = maindir.listFiles();
System.out.println(
"**********************************************" );
System.out.println(
"Files from main directory : " + maindir);
System.out.println(
"**********************************************" );
RecursivePrint(arr, 0 );
}
}
}
|
Output:
**********************************************
Files from main directory : C:\Users\Gaurav Miglani\Desktop\Test
**********************************************
Cormen.pdf
Extra-Items.pdf
XYZ.pdf
[Docs]
A.docx
B.doc
C.docx
ABC.pdf
JKL.pdf
[sheets]
XXX.csv
YYY.csv
results.pdf
[Resumes]
[Before2016]
Resume2015.doc
Resume2016.doc
[Before2014]
Resume2014.doc
Resume2017.doc
Resume2017.pdf
QA.doc
Testing.pdf
Example 3 –
Below is another iterative program to get all file name using Stack DS
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
String mainDir = "c:\\GFG\\example" ;
File file = new File(mainDir);
Stack<File> s = new Stack<>();
s.push(file);
System.out.println( "Content of Directory " + mainDir
+ " is" );
while (!s.empty()) {
File tmpF = s.pop();
if (tmpF.isFile()) {
System.out.println(tmpF.getName());
}
else if (tmpF.isDirectory()) {
File[] f = tmpF.listFiles();
for (File fpp : f) {
s.push(fpp);
}
}
}
}
}
|
Output:
Content of Directory c:\GFG\example is
example.txt
testTwo.java
testTwo.class
test.java
test.class
test.java
eg1.java
eg1.class
test.java
test.class
Students.java
Students.class
NOTE: The above code won’t compile on online IDE to compile and execute it download in your local system.
If you like GeeksforGeeks and would like to contribute, you can write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.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.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
26 Jan, 2022
Like Article
Save Article