【java】递归统计本地磁盘所有文件,提取重复文件,JDK8 map迭代
2024-08-29 03:53:04
package com.sxd.createDao; import java.io.File;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap; public class Test { private long a = 0;
Map<String, File> map = new HashMap<String, File>();
Map<String, String> repeatMap = new TreeMap<String ,String>();
@org.junit.Test
public void test() {
System.out.println(LocalDateTime.now());
//本机磁盘上所有文件
File [] files = File.listRoots();
for (File file : files) {
num(file);
}
// num(new File("D:/BaiduYunDownload")); //单个文件夹下 System.out.println("计算机文件总数:"+a);
System.out.println(LocalDateTime.now());
if(repeatMap.size() > 0){
repeatMap.forEach((k,v)->System.out.println("文件路径:"+k+" 文件名:"+v));
} } //计算文件夹下所有文件总数
public void num(File file){
if(file.isDirectory()){
File [] files = file.listFiles();
if(files != null){
for (File string : files) {
num(string);
}
}
}else{
a++;
if(map.containsKey(file.getName())){
File thisFile = map.get(file.getName());
if(file.getTotalSpace() == thisFile.getTotalSpace()){
repeatMap.put(file.getPath() , file.getName());
repeatMap.put(thisFile.getPath(),thisFile.getName());
}
}else{
map.put(file.getName(),file);
} }
}
}
最新文章
- JS高程5.引用类型(1)Object类型
- Apache服务器安装过程及问题的解决(for windows system32bit)
- 使用Auto Layout中的VFL(Visual format language)--代码实现自动布局【转】
- POJ 2689
- jQuery_效果(滑动)
- 海康、大华IpCamera RTSP地址和格式
- UVaLive 6855 Banks (水题,暴力)
- vim高亮显示
- Hackers’ Crackdown-----UVA11825-----DP+状态压缩
- js的内置对象
- EL表达式+JSTL
- How do I learn machine learning?
- tomcat的LifecycleException异常
- struts2中的namespace意义
- u-boot启动第二阶段以及界面命令分析
- Morton Code
- springboot项目,执行查询方法报错
- 与native交互时会出现的问题
- weakself的另一种写法
- Minimum Path Sum,最短路径问题,动态规划