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:
// import java.io.File;
File directory = new File("C:\\path\\to\\directory");
String[] names = directory.list(); // contains names of files and directories
Practical example
In this example, we use java.io.File
list()
method to get the list of files and directories under the given path.
import java.io.File;
import java.io.IOException;
public class Program {
public static void main(String[] args) throws IOException {
File directory = new File("C:\\path\\to\\directory");
String[] names = directory.list(); // contains names of files and directories
if (names != null) {
for (String name : names) {
System.out.println(name);
}
}
}
}