What happens if the first file this code encounters is a dir. Why does it not ignore all other files in that dir (therefore obscuring the results)?
This is taken from How do I iterate through the files in a directory in Java?
I'm not trying to dispute that this code works but how does it account for above scenario ?
public static void main(String... args) {
File[] files = new File("C:/").listFiles();
showFiles(files);
}
public static void showFiles(File[] files) {
for (File file : files) {
if (file.isDirectory()) {
System.out.println("Directory: " + file.getName());
showFiles(file.listFiles()); // Calls same method again.
} else {
System.out.println("File: " + file.getName());
}
}
}