Java-使用DirectoryStream统计文件夹中的所有文件扩展名
我想显示特定文件夹中的所有文件扩展名,并使用Java-使用DirectoryStream统计文件夹中的所有文件扩展名,java,file,lambda,path,directorystream,Java,File,Lambda,Path,Directorystream,我想显示特定文件夹中的所有文件扩展名,并使用DirectoryStream给出每个扩展名的总数 现在我只显示该文件夹中的所有文件,但如何只获取它们的扩展名呢? 我还应该获得这些文件的扩展名,并计算该文件夹中每个扩展名的总数(请参见下面的输出) 首先检查它是否是文件,如果是,则提取文件扩展名。最后,使用groupingBy收集器获取所需的字典结构。这是它的样子 try (Stream<Path> stream = Files.list(Paths.get("path/to/your/f
DirectoryStream
给出每个扩展名的总数
现在我只显示该文件夹中的所有文件,但如何只获取它们的扩展名呢?
我还应该获得这些文件的扩展名,并计算该文件夹中每个扩展名的总数(请参见下面的输出)
首先检查它是否是文件,如果是,则提取文件扩展名。最后,使用
groupingBy
收集器获取所需的字典结构。这是它的样子
try (Stream<Path> stream = Files.list(Paths.get("path/to/your/file"))) {
Map<String, Long> fileExtCountMap = stream.filter(Files::isRegularFile)
.map(f -> f.getFileName().toString().toUpperCase())
.map(n -> n.substring(n.lastIndexOf(".") + 1))
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
}
try(Stream=Files.list(path.get(“path/to/your/file”)){
Map fileExtCountMap=stream.filter(Files::isRegularFile)
.map(f->f.getFileName().toString().toUpperCase())
.map(n->n.substring(n.lastIndexOf(“.”+1))
.collect(Collectors.groupingBy(Function.identity()、Collectors.counting());
}
首先检查它是否是文件,如果是,则提取文件扩展名。最后,使用groupingBy
收集器获取所需的字典结构。这是它的样子
try (Stream<Path> stream = Files.list(Paths.get("path/to/your/file"))) {
Map<String, Long> fileExtCountMap = stream.filter(Files::isRegularFile)
.map(f -> f.getFileName().toString().toUpperCase())
.map(n -> n.substring(n.lastIndexOf(".") + 1))
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
}
try(Stream=Files.list(path.get(“path/to/your/file”)){
Map fileExtCountMap=stream.filter(Files::isRegularFile)
.map(f->f.getFileName().toString().toUpperCase())
.map(n->n.substring(n.lastIndexOf(“.”+1))
.collect(Collectors.groupingBy(Function.identity()、Collectors.counting());
}
您可以尝试以下方法:
public class FileCount {
public static void main(String[] args) throws IOException {
Path path = Paths.get(System.getProperty("user.dir"));
if (Files.isDirectory(path)) {
Map<String, Long> result = Files.list(path).filter(f -> f.toFile().isFile()).map(FileCount::getExtension)
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
System.out.println(result);
} else {
System.out.printf("Path was not found.");
}
}
public static String getExtension(Path path) {
String parts[] = path.toString().split("\\.");
if (1 < parts.length) {
return parts[parts.length - 1];
}
return path.toString();
}
公共类文件计数{
公共静态void main(字符串[]args)引发IOException{
Path Path=Path.get(System.getProperty(“user.dir”);
if(Files.isDirectory(path)){
映射结果=Files.list(path).filter(f->f.toFile().isFile()).Map(FileCount::getExtension)
.collect(Collectors.groupingBy(Function.identity()、Collectors.counting());
系统输出打印项次(结果);
}否则{
System.out.printf(“未找到路径”);
}
}
公共静态字符串getExtension(路径){
字符串部分[]=path.toString().split(“\\”);
if(1<零件长度){
返回零件[零件长度-1];
}
返回路径.toString();
}
您甚至可以返回地图,并按照您想要的方式排列结果。您可以尝试以下方法:
public class FileCount {
public static void main(String[] args) throws IOException {
Path path = Paths.get(System.getProperty("user.dir"));
if (Files.isDirectory(path)) {
Map<String, Long> result = Files.list(path).filter(f -> f.toFile().isFile()).map(FileCount::getExtension)
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
System.out.println(result);
} else {
System.out.printf("Path was not found.");
}
}
public static String getExtension(Path path) {
String parts[] = path.toString().split("\\.");
if (1 < parts.length) {
return parts[parts.length - 1];
}
return path.toString();
}
公共类文件计数{
公共静态void main(字符串[]args)引发IOException{
Path Path=Path.get(System.getProperty(“user.dir”);
if(Files.isDirectory(path)){
映射结果=Files.list(path).filter(f->f.toFile().isFile()).Map(FileCount::getExtension)
.collect(Collectors.groupingBy(Function.identity()、Collectors.counting());
系统输出打印项次(结果);
}否则{
System.out.printf(“未找到路径”);
}
}
公共静态字符串getExtension(路径){
字符串部分[]=path.toString().split(“\\”);
if(1<零件长度){
返回零件[零件长度-1];
}
返回路径.toString();
}
您甚至可以返回映射并按您想要的方式排列结果。定义“文件扩展名”。该定义应提供获取该映射的算法。定义“文件扩展名”。该定义应提供获取该映射的算法。您可以将
.getName()
更改为.getName().toUpperCase()
或添加映射(String::toUpperCase)
为了避免foo.exe
和foo.exe
的问题,收集器将exe
和exe
作为单独的扩展名进行计数。感谢您的建议。我已经更新了答案。我将使用stream.filter,而不是恢复到过时的java.io.File类(path->Files.isRegularFile(path))
。类似地,映射(s->s.getFileName().toString().toUpperCase())
@VGR使用方法引用可以进一步改进前者。我已经更新了答案。感谢您的建议。感谢您的帮助!现在,在我的情况下,如何使用DirectoryStream实现您的解决方案?filter()、getFileName()、substring()等方法在DirectoryStream上不起作用。您可以更改.getName()
到.getName().toUpperCase()
或添加映射(字符串::toUpperCase)
为了避免foo.exe
和foo.exe出现问题,收集器将exe
和exe
作为单独的扩展名进行计数。感谢您的建议。我已更新了答案。我将使用stream.filter(路径->Files.isRegularFile(路径))而不是恢复到过时的java.io.File类
。类似地,map(s->s.getFileName().toString().toUpperCase())
@VGR使用方法引用可以进一步改进前者。我已经更新了答案。感谢您的建议。感谢您的帮助。现在,在我的情况下,如何使用DirectoryStream实现您的解决方案?方法如filter(),getFileName(),子字符串()不适用于directoryStream。