在java1.7以前,采用如下的方式:
public static void listFilesForFolder(final File folder) {
for (final File fileEntry : folder.listFiles()) {
if (fileEntry.isDirectory()) {
listFilesForFolder(fileEntry);
} else {
System.out.println(fileEntry.getName());
}
}
}
在java1.8以后,可以采用如下的方式:
try(Stream<Path> paths = Files.walk(Paths.get("C:/Users/Administrator/Desktop"))) {
paths.forEach(filePath -> {
if (Files.isRegularFile(filePath)) {
System.out.println(filePath);
}
});
} catch (IOException e) {
e.printStackTrace();
}