In Java 8 you can do this
Files.walk(Paths.get("/path/to/folder")) .filter(Files::isRegularFile) .forEach(System.out::println); which will print all files in a folder while excluding all directories. If you need a list, the following will do:
Files.walk(Paths.get("/path/to/folder")) .filter(Files::isRegularFile) .collect(Collectors.toList()) If you want to return List<File> instead of List<Path> just map it:
List<File> filesInFolder = Files.walk(Paths.get("/path/to/folder")) .filter(Files::isRegularFile) .map(Path::toFile) .collect(Collectors.toList()); You also need to make sure to close the stream! Otherwise you might run into an exception telling you that too many files are open. Read Read here for more.here for more information.