Java nio provides so many useful and very quick methods using we can perform our work very easy. One of them is Files.newDirectoryStream method which will return all files from directory.

Here is my local directory:

Java read all files in directory

Java read all files in directory

1. Java read all files in directory using newDirectoryStream

Here is example of Java read all files in directory using Java 8 nio. Path is object of directory from which we want to read all files. It will return DirectoryStream<Path> object which return all files in directory.

try {
          Path dirPath = Paths.get("F:\\application\\start.do\\frontier");            // create directory path
          try (DirectoryStream<Path> dirPaths = Files
                  .newDirectoryStream(dirPath)) {            // get all files from directory
              for (Path file : dirPaths) {
                  System.out.println(file.getFileName().toString());                  // print all files names
              }
          }
      } catch (Exception e) {
          e.printStackTrace();
      }

Output

00000000.jdb
je.info.0
je.info.0.lck
je.lck

2. Java read all files in directory with specific type (newDirectoryStream + file type)

We can pass second argument in Files.newDirectoryStream with file type or extension  separated by comma like “*{c,java,class}” so it will return specific files types only.

try {
           Path dirPath = Paths.get("F:\\application\\start.do\\frontier");            // create directory path
           try (DirectoryStream<Path> dirPaths = Files
                   .newDirectoryStream(dirPath,"*.{jdb}")) {            // get all files from directory and .jdb only
               for (Path file : dirPaths) {
                   System.out.println(file.getFileName().toString());                  // print all files names
               }
           }
       } catch (Exception e) {
           e.printStackTrace();
       }

Output:

00000000.jdb

3. Java read all files in directory with filter (newDirectoryStream + filter)

We can pass second argument in Files.newDirectoryStream Filter which return boolean value based on that files will be return.

try {
            Path dirPath = Paths.get("F:\\application\\start.do\\frontier");            // create directory path
            try (DirectoryStream<Path> dirPaths = Files
                   .newDirectoryStream(dirPath, entry -> entry.getFileName().toString().startsWith("je"))) { // get all files from directory start with je
                for (Path file : dirPaths) {
                    System.out.println(file.getFileName().toString());                  // print all files names
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

Output:

je.info.0
je.info.0.lck
je.lck

References :

https://docs.oracle.com/javase/8/docs/api/java/nio/file/DirectoryStream.html

https://docs.oracle.com/javase/tutorial/essential/io/pathOps.html

 

 

Was this post helpful?
Let us know, if you liked the post. Only in this way, we can improve us.
Yes
No

Leave a Reply

Your email address will not be published. Required fields are marked *