-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecursiveFileTest.java
More file actions
42 lines (38 loc) · 924 Bytes
/
RecursiveFileTest.java
File metadata and controls
42 lines (38 loc) · 924 Bytes
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
import java.lang.*;
import java.io.File;
/**
* Program is recursively listing files in a given directory.
*
* To run it type: java RecursiveFileTest /absolute/path
**/
public class RecursiveFileTest {
private static int lvl = 0;
public static void main(String[] args) {
File baseFile;
if (args.length > 0)
baseFile = new File(args[0]);
else
baseFile = new File(".");
listFiles(baseFile);
System.exit(0);
}
public static void listFiles(File baseFile) {
if(baseFile.isDirectory()) {
for (int i = 0; i < lvl; i++)
System.out.print(' ');
System.out.print(baseFile.getName() + '/' + '\n');
lvl++;
// including indentation
File[] filesInside = baseFile.listFiles();
for(File file : filesInside) {
listFiles(file);
}
if (lvl > 0) lvl--;
}
else {
for (int i = 0; i < lvl; i++)
System.out.print(' ');
System.out.print(baseFile.getName() + '\n');
}
}
}