The Java 7 java.nio.file can be used to improve performance.
iterators
The DirectoryStream<T> interface can be used to search through a directory without first loading its contents into memory. Although the old API creates an array of all the file names in the folder, the new approach loads every file name (or a group of limited size cached file names) when it occurs during iteration.
To get an instance representing the specified Path , Files.newDirectoryStream(Path) can be called. I suggest you use the try-with-resources statement to close the stream correctly, but if you cannot, remember to do it manually at the end using DirectoryStream<T>.close() .
Path folder = Paths.get("..."); try (DirectoryStream<Path> stream = Files.newDirectoryStream(folder)) { for (Path entry : stream) {
Filters
The DirectoryStream.Filter<T> interface can be used to skip groups of records during iteration.
Since it is @FunctionalInterface , starting in Java 8, you can implement it with a lambda expression, overriding Filter<T>.accept(T) , which decides whether this directory entry should be accepted or filtered. Then you use the static method Files.newDirectoryStream(Path, DirectoryStream.Filter<? super Path>) with the newly created instance. Or you can use the static method Files.newDirectoryStream(Path, String) instead, which you can use to easily match the file name.
Path folder = Paths.get("..."); try (DirectoryStream<Path> stream = Files.newDirectoryStream(folder, "*.txt")) { for (Path entry : stream) {
Path folder = Paths.get("..."); try (DirectoryStream<Path> stream = Files.newDirectoryStream(folder, entry -> Files.isDirectory(entry))) { for (Path entry : stream) {
Francesco menzani
source share