-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecursiveFileDisplay.java
More file actions
64 lines (52 loc) · 1.82 KB
/
RecursiveFileDisplay.java
File metadata and controls
64 lines (52 loc) · 1.82 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;
public class RecursiveFileDisplay {
public static void main(String[] args) throws IOException {
File currentDir = new File("C:\\Users\\ankur.singhal\\git\\ps-customer-app\\www");
List<String> dirPath = new ArrayList<String>();
displayDirectoryContents(currentDir, dirPath);
getFilesFromLongestFolder(dirPath);
}
private static void displayDirectoryContents(File dir, List<String> dirPath) {
try {
File[] files = dir.listFiles();
for (File file : files) {
if (file.isDirectory()) {
dirPath.add(file.getCanonicalPath());
displayDirectoryContents(file, dirPath);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static void getFilesFromLongestFolder(List<String> dirPath) throws IOException {
SortedMap<Long, List<String>> longestFoldersPath = new TreeMap<Long, List<String>>();
for (String folderPath : dirPath) {
Long count = folderPath.chars().filter(ch -> ch == '\\').count();
if (longestFoldersPath.containsKey(count)) {
List<String> existingFolders = longestFoldersPath.get(count);
existingFolders.add(folderPath);
} else {
List<String> newFolders = new ArrayList<String>();
newFolders.add(folderPath);
longestFoldersPath.put(count, newFolders);
}
}
// Longest Folder path
Long key = longestFoldersPath.lastKey();
List<String> longestFolders = longestFoldersPath.get(key);
for (String path : longestFolders) {
File currentDir = new File(path);
System.out.println("Folder with Longest Path = " + currentDir.getCanonicalPath());
File[] files1 = currentDir.listFiles();
for (File file : files1) {
System.out.println("File Name : " + file.getName());
}
}
}
}