-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileFolderList.java
More file actions
70 lines (60 loc) · 1.97 KB
/
FileFolderList.java
File metadata and controls
70 lines (60 loc) · 1.97 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
65
66
67
68
69
70
import java.io.File;
public class FileFolderList {
public static void main(String[] args) {
List<File> list = new ArrayList<>();
String name=getFileFromFolders("BIGFILE", list, "ABCDFILE.TXT", null);
System.out.println(name);
PrintFile(name);
}
// only 1 layer folder
static void fileFolderList() {
File folder = new File(".");
File[] fList = folder.listFiles();
for (File file: fList) {
if(file.isDirectory()) {
System.out.println("["+file.getName()+"]");
}
else {
System.out.println(file.getName());
}
}
}
// subfolder list
static void listFileFolder(String directoryName, List<File> files) {
File directory = new File(directoryName);
// Get all files from a directory.
File[] fList = directory.listFiles();
if(fList != null) {
for (File file : fList) {
if (file.isFile()) {
System.out.println(file.getName());
files.add(file);
} else if (file.isDirectory()) {
System.out.println("["+file.getName()+"]");
listFileFolder(file.getAbsolutePath(), files);
}
}
}
}
//get filepath from subfolders
static String getFileFromFolders(String directoryName, List<File> files, String fileToFind, String filePath) {
File directory = new File(directoryName);
// Get all files from a directory.
File[] fList = directory.listFiles();
if(fList != null) {
for (File file : fList) {
if (file.isFile()) {
System.out.println(file.getName());
if(file.getName().equals(fileToFind)) {
return file.getPath();
}
files.add(file);
} else if (file.isDirectory()) {
System.out.println("["+file.getName()+"]");
filePath=getFileFromFolders(file.getPath(), files, fileToFind, filePath);
}
}
}
return filePath;
}
}