-
Notifications
You must be signed in to change notification settings - Fork 187
/
DisplayFile.java
44 lines (44 loc) · 1.33 KB
/
DisplayFile.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
// import statement
import java.io.File;
public class DisplayFileExample
{
public void printFileNames(File[] a, int i, int lvl)
{
// base case of the recursion
// i == a.length means the directory has
// no more files. Hence, the recursion has to stop
if(i == a.length)
{
return;
}
// checking if the encountered object is a file or not
if(a[i].isFile())
{
System.out.println(a[i].getName());
}
// recursively printing files from the directory
// i + 1 means look for the next file
printFileNames(a, i + 1, lvl);
}
// Main Method
public static void main(String[] argvs)
{
// Providing the full path for the directory
String path = "E:\\Documents";
// creating a file object
File fObj = new File(path);
// creating on object of the class DisplayFileExample
DisplayFileExample obj = new DisplayFileExample();
if(fObj.exists() && fObj.isDirectory())
{
// array for the files of the directory pointed by fObj
File a[] = fObj.listFiles();
// display statements
System.out.println("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =");
System.out.println("Displaying Files from the directory : " + fObj);
System.out.println("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =");
// Calling the method
obj.printFileNames(a, 0, 0);
}
}
}