gpt4 book ai didi

java - 在Java中如何计算目录中具有相同文件扩展名的文件数量?

转载 作者:行者123 更新时间:2023-12-02 10:27:01 25 4
gpt4 key购买 nike

我希望能够计算用户创建的所有保存游戏。

使用Java,如何统计目录中具有特定扩展名的所有文件?

此代码对所有文件进行计数,无论扩展名如何:

public class MCVE {

public static void main(String[] args) {
countFiles();
}

private static void countFiles() {
long amountOfFiles = 0;
try {
Stream<Path> files = Files.list(Paths.get("./saves"));
amountOfFiles = files.count();
System.out.println(amountOfFiles);
} catch (IOException e) {
e.printStackTrace();
}
}
}

最佳答案

在此函数中传递您的扩展。

amountOfFiles = files.map(Path::toFile).filter(e->e.getName().endsWith(".xml")).count();

关于java - 在Java中如何计算目录中具有相同文件扩展名的文件数量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53878959/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com