Java Examples - Find Files in a Specified Directory
The following example demonstrates using the File class's dir.list() method to find all file lists in a specified directory:
Main.java File
import java.io.File;
public class Main {
public static void main(String[] argv) throws Exception {
File dir = new File("../java");
String[] children = dir.list();
if (children == null) {
System.out.println("The directory does not exist");
}
else {
for (int i = 0; i < children.length; i++) {
String filename = children[i];
System.out.println(filename);
}
}
}
}
The output of running the above code is:
Car.class FileUtil.class FileUtil.java HelloWorld.class HelloWorld.java HelloWorldDebug.class HelloWorldDebug.java ……Other Extensions
Java Examples