如何通过Java读取文件夹中的所有文件?
public void listFilesForFolder(final File folder) { for (final File fileEntry : folder.listFiles()) { if (fileEntry.isDirectory()) { listFilesForFolder(fileEntry); } else { System.out.println(fileEntry.getName()); } } } final File folder = new File("/home/you/Desktop"); listFilesForFolder(folder);
Java 8提供了Files.walk API。
try (Stream<Path> paths = Files.walk(Paths.get("/home/you/Desktop"))) { paths .filter(Files::isRegularFile) .forEach(System.out::println); }