Java 8 : Get files from folder/subfolder

标签 java collections java-8 functional-programming stream

<分区>

我在 SpringBoot 应用程序的资源文件夹中有这个文件夹。

resources/files/a.txt
resources/files/b/b1.txt
resources/files/b/b2.txt
resources/files/c/c1.txt
resources/files/c/c2.txt

我想获取所有的txt文件,所以这是我的代码:

   ClassLoader classLoader = this.getClass().getClassLoader();
   Path configFilePath = Paths.get(classLoader.getResource("files").toURI());   

   List<Path> atrackFileNames = Files.list(configFilePath)
                .filter(s -> s.toString().endsWith(".txt"))
                .map(Path::getFileName)
                .sorted()
                .collect(toList());

但我只得到文件a.txt

最佳答案

    Path configFilePath = FileSystems.getDefault()
            .getPath("C:\\Users\\sharmaat\\Desktop\\issue\\stores");

    List<Path> fileWithName = Files.walk(configFilePath)
            .filter(s -> s.toString().endsWith(".java"))
            .map(Path::getFileName).sorted().collect(Collectors.toList());

    for (Path name : fileWithName) {
        // printing the name of file in every sub folder
        System.out.println(name);
    }

关于Java 8 : Get files from folder/subfolder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48563709/

相关文章:

java - 在 Android 上使用 SoundPool 播放声音,同时使用 AudioRecord 录制麦克风

java - 终止从线程启动的 Java 进程

java - 从数组中的三个整数中找到可以得到的最高乘积 - 如何使用蛮力求解

java - 使用 "My files"应用程序单击按钮打开文件夹

java - 添加通用集合排序方法时出错

Java 集合在提供引用的同时确保唯一性

scala - 惯用 "do until"集合更新

java - 使用流进行字数统计

Java 流收集函数 : property cannot be resolved [Compilation error]

java - Java 中是否有可能从 2 个 Stream 中创建一个新 Stream?