Increase directory contents faster (alternatives to java.io.File)

I have been using the old, deprecated java.io.File.listFiles() too long.

Performance is not so good. It:

  • Expensive as it creates a new File object for each entry.
  • Slowly because you need to wait for the array to complete before processing starts.
  • Very bad, especially if you only need to work with a subset of the content.

What are the alternatives?

+7
java java-7 file-io directory-content
source share
1 answer

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) { // Process the entry } } catch (IOException ex) { // An I/O problem has occurred } 

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) { // The entry can only be a text file } } catch (IOException ex) { // An I/O problem has occurred } 

 Path folder = Paths.get("..."); try (DirectoryStream<Path> stream = Files.newDirectoryStream(folder, entry -> Files.isDirectory(entry))) { for (Path entry : stream) { // The entry can only be a directory } } catch (IOException ex) { // An I/O problem has occurred } 
+15
source share

All Articles