EN
Java - get list of files and directories from directory
3 points
In this article, we would like to show you how to get the list of files and directories from the directory specified by its path in Java.
Quick solution:
xxxxxxxxxx
1
// import java.io.File;
2
3
File directory = new File("C:\\path\\to\\directory");
4
String[] names = directory.list(); // contains names of files and directories
In this example, we use java.io.File
list()
method to get the list of files and directories under the given path.
xxxxxxxxxx
1
import java.io.File;
2
import java.io.IOException;
3
4
public class Program {
5
6
public static void main(String[] args) throws IOException {
7
8
File directory = new File("C:\\path\\to\\directory");
9
String[] names = directory.list(); // contains names of files and directories
10
if (names != null) {
11
for (String name : names) {
12
System.out.println(name);
13
}
14
}
15
}
16
}