0

How to iterate through all files by mask in Java? For example, there's a mask with wild cards like

D:\work\mytestfolder\temp\*.txt

Need to get a collection (or iterator or whatever else) of all *.txt files in the directory above.

Some more details. Want to process a number of files and it's convenient here to define a set of masks like those shown above. The GLOBE syntax (https://docs.oracle.com/javase/7/docs/api/java/nio/file/FileSystem.html#getPathMatcher(java.lang.String)) looks very helpful and would desirably be supported.

Nick Legend
  • 789
  • 1
  • 7
  • 21
  • 3
    what have you already tried? – inxoy Oct 26 '17 at 12:41
  • Now struggling with this https://docs.oracle.com/javase/tutorial/essential/io/find.html. Seems too much complicated for so simple task. – Nick Legend Oct 26 '17 at 12:45
  • @Alex, it's not actually that bad. In Java8 you have `Files::walk` which streams entities to you. So you split your mask on first wildcard, build a path from first part and a PathMatcher from second part, then start walking from the Path and filter the stream using PathMatcher. – M. Prokhorov Oct 26 '17 at 12:56
  • have had a look at https://stackoverflow.com/questions/3057621/java-filenames-filter-pattern. Not exactly what I need. Of cause we can implement our own solutions but I'd like to find something standard first. Also our hand-made solutions would desirably handle platform specifics, doesn't look like a simple task :( – Nick Legend Oct 26 '17 at 13:13

2 Answers2

1

No need to create an explicit PathMatcher. Just use Files.newDirectoryStream:

try (DirectoryStream<Path> dir = Files.newDirectoryStream(
        Paths.get("D:\\work\\mytestfolder\\temp"), "*.txt")) {

    for (Path entry : dir) {
         // ...
    }
}
VGR
  • 40,506
  • 4
  • 48
  • 63
  • It works and is convenient enough, thanks! Just need to get it to iterate also through the subdirectories of the starting directory (recursively). – Nick Legend Oct 26 '17 at 14:25
  • For recursive traversal, you’ll want [Files.walk](https://docs.oracle.com/javase/9/docs/api/java/nio/file/Files.html#walk-java.nio.file.Path-java.nio.file.FileVisitOption...-) or [Files.walkFileTree](https://docs.oracle.com/javase/9/docs/api/java/nio/file/Files.html#walkFileTree-java.nio.file.Path-java.nio.file.FileVisitor-). Neither has a file mask option, but obviously it’s easy enough to check each file yourself. The former method returns a Stream, so you can do `Files.walk(dir).filter(p -> !Files.isDirectory(p) && p.toString().endsWith(".txt"))`. – VGR Oct 26 '17 at 15:24
  • Cannot use Java 8, at least for now. So `Files.walkFileTree` with explicit `PathMatcher` seems to be the best choice... though really too verbose :( – Nick Legend Oct 26 '17 at 15:38
-1
File dir = new File("E:/Test");
File [] files = dir.listFiles(new FilenameFilter() {
   @Override
   public boolean accept(File dir, String name) {
      return name.endsWith(".chm");
   }
});

for (File xmlfile : files) {
    System.out.println(xmlfile);
}
jai
  • 98
  • 8