Java实现根据文件哈希值和文件大小,删除重复文件

您所在的位置:网站首页 excel如何找出重复项并删除 Java实现根据文件哈希值和文件大小,删除重复文件

Java实现根据文件哈希值和文件大小,删除重复文件

2023-06-03 08:50| 来源: 网络整理| 查看: 265

前言

微信接收的文件里,经常会有重复文件,时间久了非常占用磁盘空间。然后自己手动去找出重复文件并删除太麻烦了,所以还是自己写个工具来帮我们做这件事。代码也很简单,主要就是要获取文件的哈希值了。

import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.HashMap; import java.util.Map; import java.util.Scanner; /** * 清除指定文件夹下的重复文件 */ public class RemoveDuplicateFile { /** 用Map来存储文件哈希值和文件大小的组合,以便判断是否为重复文件 */ private static Map fileMap = new HashMap(); /** * 获取文件哈希值 */ private static String getFileHash(File file) { try { MessageDigest md = MessageDigest.getInstance("MD5"); FileInputStream fis = new FileInputStream(file); byte[] buffer = new byte[1024]; int length; while ((length = fis.read(buffer)) != -1) { md.update(buffer, 0, length); } fis.close(); byte[] digest = md.digest(); StringBuilder sb = new StringBuilder(); for (byte b : digest) { sb.append(String.format("%02x", b)); } return sb.toString(); } catch (NoSuchAlgorithmException | IOException e) { e.printStackTrace(); return null; } } /** * 根据文件的哈希值和文件大小,判断文件是否为重复文件 * @param folderPath 文件夹路径 */ public static void removeDuplicateFile(String folderPath){ // 获取文件夹下的所有文件 File folder = new File(folderPath); File[] files = folder.listFiles(); for (File file : files) { if (file.isFile()) { String fileHash = getFileHash(file); long fileSize = file.length(); String key = fileHash + "_" + fileSize; if (fileMap.containsKey(key)) { // 如果已经存在相同哈希值和文件大小的文件,则删除当前文件 file.delete(); System.out.println("文件 " + file.getName() + " 是重复文件!已经删除!"); } else { fileMap.put(key, fileSize); } }else { // 递归子文件或文件夹 removeDuplicateFile(file.getAbsolutePath()); } } } public static void main(String[] args) { System.out.println("请输入要清除重复文件的文件夹路径:"); Scanner sc = new Scanner(System.in); String folderPath = sc.next(); removeDuplicateFile(folderPath); } }

我把代码打包为exe程序了,方便使用。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3