Java Stream How to - Find files under a folder by extension and sort








Question

We would like to know how to find files under a folder by extension and sort.

Answer

//from  ww  w.java 2 s .c  o m

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Main {

  public static void main(String[] args) throws Exception{
    Path start = Paths.get("c:/Java_Dev/");
    int maxDepth = 5;
    Files
        .find(start, maxDepth, (path, attr) ->String.valueOf(path).endsWith("xls"))
        .sorted()
        .forEach(System.out::println);
  }
}