Alexis Lopez

About Alexis Lopez

Java Lover, certified as Java Programmer, Mobile Application Developer and Web Component Developer.

Java 7: File Filtering using NIO.2 – Part 1

NIO.2 is a new API for I/O operations included in the JDK since Java 7. With this new API, you can perform the same operations performed with

java.io plus a lot of great functionalities such as: Accessing file metadata and watching for directory changes, among others. Obviously, the java.io package is not going to disappear because of backward compatibility, but we are encouraged to start using NIO.2 for our new I/O requirements. In this post, we are going to see how easy it is to filter the contents of a directory using this API. There are 3 ways in order to do so, that’s why this post is Part1.

What you need

NetBeans 7+ or any other IDE that supports Java 7
JDK 7+

Filtering content of a directory is a common task in some applications and NIO.2 makes it really easy. The classes and Interfaces we are going to use are described next:

  • java.nio.file.Path: Interface whose objects may represent files or directories in a file system. It’s like the java.io.File but in NIO.2. Whatever I/O operation you want to perform, you need an instance of this interface.
  • java.nio.file.DirectoryStream: Interface whose objects iterate over the content of a directory.
  • java.nio.file.Files: Class with static methods that operates on files, directories, etc.

The way we are going to filter the contents of a directory is by using glob patterns, which are like regular expressions but simpler. The pattern is matched against the name of the files, directories, etc. That live inside the directory. This is important to remember, using this method you can only filter by the name of the file, directory, etc.

For more information about globbing,
check this wiki. Also, there is some documentation about globbing patterns in the
Java Docs.

So, let’s suppose that we have a directory called Images, and we need to iterate over the files inside this directory, but we only need the .png files. We have to follow this steps in order to do so:

  1. Obtain a java.nio.file.Path instance which points to the directory Images.
  2. Open a new java.nio.file.DirectoryStream using the java.nio.file.Files class and passing the directory and the pattern (*.png) as parameters.
  3. Iterate over the content of the directory using the java.nio.file.DirectoryStream instance.

Next is the source code of a method that scans a directory using the pattern passed as parameter:

//in a class...

    /**
     * Scans the directory using the glob pattern passed 
     * as parameter. 
     * @param folder directory to scan
     * @param pattern glob pattern (filter)
     */
    private static void scan(String folder, String pattern) {
        //obtains the Images directory in the app directory
        Path dir = Paths.get(folder);
        //the Files class offers methods for validation
        if (!Files.exists(dir) || !Files.isDirectory(dir)) {
            System.out.println('No such directory!');
        }
        //Try with resources... so nice!
        try (DirectoryStream

                       ds = 
                    Files.newDirectoryStream(dir, pattern)) {
            //iterate over the content of the directory
            int count = 0;
            for (Path path : ds) {
                System.out.println(path.getFileName());
                count++;
            }
            System.out.println();
            System.out.printf('%d Files match the pattern'
                                                     , count);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

Following is the result of the execution on my windows machine:

And on my Linux virtual machine:

That’s something I love from Java, Write once, run everywhere! I hope you enjoyed this post, there are more ways to filter the content of a directory and we’ll explore them in future posts.

Click here to download the complete source code.
 

Reference: Java 7: File Filtering using NIO.2 – Part 1 from our JCG partner Alexis Lopez at the Java and ME blog.

Related Whitepaper:

Bulletproof Java Code: A Practical Strategy for Developing Functional, Reliable, and Secure Java Code

Use Java? If you do, you know that Java software can be used to drive application logic of Web services or Web applications. Perhaps you use it for desktop applications? Or, embedded devices? Whatever your use of Java code, functional errors are the enemy!

To combat this enemy, your team might already perform functional testing. Even so, you're taking significant risks if you have not yet implemented a comprehensive team-wide quality management strategy. Such a strategy alleviates reliability, security, and performance problems to ensure that your code is free of functionality errors.Read this article to learn about this simple four-step strategy that is proven to make Java code more reliable, more secure, and easier to maintain.

Get it Now!  

Leave a Reply


1 × three =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

15,153 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books
Get tutored by the Geeks! JCG Academy is a fact... Join Now
Hello. Add your message here.