java如何找出特定扩展名的文件

通过文件扩展迭代目录并过滤文件:

public void iterateAndFilter() throws IOException {
     Path dir = Paths.get("C:/foo/bar");
     PathMatcher imageFileMatcher =
        FileSystems.getDefault().getPathMatcher(
             "regex:.*(?i:jpg|jpeg|png|gif|bmp|jpe|jfif)");
     try (DirectoryStream stream = Files.newDirectoryStream(dir,
           entry -> imageFileMatcher.matches(entry.getFileName()))) {
      for (Path path : stream) {
           System.out.println(path.getFileName());
    }
}
}
日期:2020-06-02 22:15:21 来源:oir作者:oir