我想问如何从目录中存在的子文件夹中读取(.xxx)类型的文件。
我有一个包含子文件夹的文件夹(子文件夹的数量未知)
C:\MainFolder
contains-> [subFolder_A][subFolder_B]......[subFolder_n]
每个子文件夹都包含大量文件,但其中一个文件具有不同的类型,特别是 (.fingerprint) 类型。
[subFolder_A] --> a.txt , atitle.txt , afin.fingerprint
[subFolder_B] --> b.txt , btitle.txt , bfin.fingerprint
...............................................
[subFolder_n] --> n.txt , ntitle.txt , nfin.fingerprint
所以我想每次读取这个文件之一到java ...执行一些功能,然后执行下一个,直到 C:\MainFolder 被完全扫描。
如果我直接给出代码的路径,我可以阅读其中一个,但如果有人知道如何以自动方式获取它们,我需要一些提示
Path path = Paths.get("C:\\MainFolder\\afin.fingerprint");
byte[] data;
byte[] fin;
try {
this.data = Files.readAllBytes(path);
byte[] data = Files.readAllBytes(path);
} catch (Exception e) {....}
fin=data;
最佳答案
您可以walk the file tree recursively with a stream ,例如这样:
Files.walk(Paths.get("C:/MainFolder"))
.filter(p -> p.toFile().isFile())
.filter(p -> p.getFileName().toString().endsWith(".fingerprint"))
.forEach(p -> process(p));
关于java - JAVA中如何从目录的子文件夹中读取特定类型的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47039558/