我在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
发布于 2018-02-01 13:22:39
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);
}发布于 2018-02-01 13:26:49
Files.list(path)方法只返回目录中的文件流。并且该方法列表不是递归的。
相反,您应该使用Files.walk(path)。此方法遍历根植于给定起始目录的所有文件树。
更多关于这方面的情况:
https://stackoverflow.com/questions/48563709
复制相似问题