Recursively list files in Java

后端 未结 27 2016
走了就别回头了
走了就别回头了 2020-11-22 00:29

How do I recursively list all files under a directory in Java? Does the framework provide any utility?

I saw a lot of hacky implementations. But none from the fra

27条回答
  •  余生分开走
    2020-11-22 00:48

    Example outputs *.csv files in directory recursive searching Subdirectories using Files.find() from java.nio:

    String path = "C:/Daten/ibiss/ferret/";
        logger.debug("Path:" + path);
        try (Stream fileList = Files.find(Paths.get(path), Integer.MAX_VALUE,
                (filePath, fileAttr) -> fileAttr.isRegularFile() && filePath.toString().endsWith("csv"))) {
            List someThingNew = fileList.sorted().map(String::valueOf).collect(Collectors.toList());
            for (String t : someThingNew) {
                t.toString();
                logger.debug("Filename:" + t);
            }
    
        }
    

    Posting this example, as I had trouble understanding howto pass the filename parameter in the #1 example given by Bryan, using foreach on Stream-result -

    Hope this helps.

提交回复
热议问题