Listing and filtering directory contents in NIO.2

There hasn’t been much happening in the area of listing directory contents until the release of Java 7. But since NIO.2 introduced a new way to do this it might be worth it to cover this area. One of big pluses of NIO.2 is the ability to use listing and filtering at once in one method call. This provides an elegant solution to most of listing/filtering needs related to work with a file system.

Listing root directories

Unless we are working with relative paths we need to be aware of the environment where our application lives, so we can define absolute paths. Since file systems are usually hierarchical structures there is at least one root directory. To properly address files and directories we need to be able to list all these root directories. To do this, we turn to the FileSystem instance itself to use its method getRootDirectories, which is an alternative to Java 6 construct File.listRoots().

*Please note that class Sets is not part of JDK, but comes from Google’s Guava library. I used it here, just for convenience to get nicely formated string representation of root directories.

With following output:

Listing and filtering directory contents

Standard task when working with file system is to list or filter files within given directory. We might need to modify, analyze or simply list them – whatever the reason, class java.nio.file.Files has our backs. It offers three variants of method newDirectoryStream that return object of type DirectoryStream<Path> to allow us to iterate over the entries in a directory. Here we see an apparent difference between current and prior versions of IO library (returning simple arrays) preventing NullPointerException. Following example shows how simple it is to list contents of given directory:

Please notice the use of isDirectory checking method that prevents NotDirectoryException. Also note the use of the try-with-resources construct – DirectoryStream is both AutoCloseable and Closeable (meaning it needs to be closed at some time) so try-with-resources comes in handy. Code returns following output:

To ensure universal usability of DirectoryStream<Path> we can filter using two basic mechanisms:

  • newDirectoryStream(Path dir, String glob)
    • Filtering using GLOB
  • newDirectoryStream(Path dir, DirectoryStream.Filter filter)
    • Filtering using  DirectoryStream.Filter

Filtering with GLOB pattern

First of all we need to know what a GLOB is. GLOB patterns are string expressions that follow specific syntax rules and they are used for matching purposes. Please refer to the following article for more information on GLOBs and GLOB syntax. When it comes to filtering using GLOBs, Files class provides us with an easy way to do so. Lets take a look at following example.

With following output:

Filtering with DirectoryStream.Filter

When the task at hand requires more complex filtering options rather than just simple file name matching, we need to implement interface DirectoryStream.Filter<Path>. This is the most powerful filtering option available at our disposal since we have an access to the rest of the application and might use third party libraries. Following example shows such a situation with two filtering conditions:

  • File size must be an even number
  • Time of execution in milliseconds must be an even number

With following output:

*Please note that based on used conditions filtered files may differ per execution.

If you enjoyed this post, then make sure you subscribe to my Newsletter Newsletter and/or Feed Feed

Posted in NIO.2 Tagged with: , , , , , ,
0 comments on “Listing and filtering directory contents in NIO.2
2 Pings/Trackbacks for "Listing and filtering directory contents in NIO.2"
  1. […] 引自:在NIO.2中列举和过滤目录内容,来自我们的JCG合作伙伴Jakub Stas在Jakub Stas的博客。 […]

  2. […] 引自:在NIO.2中枚举和过滤目录内容,来自我们的JCG协作毛病Jakub Stas在Jakub Stas的博客。 […]

Leave a Reply