-
Notifications
You must be signed in to change notification settings - Fork 0
/
GetFilesAndFoldersInsideDirectory.java
51 lines (42 loc) · 1.24 KB
/
GetFilesAndFoldersInsideDirectory.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
45
46
47
48
49
50
51
package com.javamultiplex.filehandling;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
public class GetFilesAndFoldersInsideDirectory {
public static void main(String[] args) throws IOException {
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter full directory path in this format [drive:\\\\directory] example - c:\\\\abc");
String path = br.readLine();
if (isValidPath(path)) {
File file = new File(path);
if (file.exists()) {
// Getting all files and folders.
String[] files = file.list();
System.out.println("Files and folders :");
for (String name : files) {
System.out.println(name);
}
} else {
System.out.println("Path doesn't exist.");
}
} else {
System.out.println("Please enter path in format [drive:\\\\directory]");
}
} finally {
if (br != null) {
br.close();
}
}
}
private static boolean isValidPath(String path) {
String pattern = "^[a-zA-Z]:\\\\\\\\[\\w]*$";
boolean result = false;
if (path.matches(pattern)) {
result = true;
}
return result;
}
}