If you can use JDK 7, the recommended way (so to speak):
public static void main(String[] args) throws IOException { Path dir = Paths.get("c:/some_dir"); try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "*.{gif,png,jpg}")) { for (Path entry: stream) { System.out.println(entry); } } }
This is more efficient because you have an iterator that does not necessarily contain all the records.
source share