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; public class DuplicateFileFinder { public static void main(String[] args) throws IOException, NoSuchAlgorithmException { String directoryPath = "your/directory/path"; File directory = new File(directoryPath); // 使用 HashMap 存储文件的哈希值和路径 Map<String, String> fileHashes = new HashMap<>(); // 遍历目录中的文件 findDuplicateFiles(directory, fileHashes); } // 遍历文件夹,递归查找文件 public static void findDuplicateFiles(File directory, Map<String, String> fileHashes) throws IOException, NoSuchAlgorithmException { if (directory.isDirectory()) { // 获取目录中的所有文件 File[] files = directory.listFiles(); for (File file : files) { findDuplicateFiles(file, fileHashes); // 递归调用,遍历子目录 } } else { // 对每个文件计算哈希值并比较 String fileHash = calculateFileHash(directory); if (fileHashes.containsKey(fileHash)) { System.out.println("重复文件:" + directory.getAbsolutePath() + " 和 " + fileHashes.get(fileHash)); } else { fileHashes.put(fileHash, directory.getAbsolutePath()); } } } // 计算文件的哈希值 public static String calculateFileHash(File file) throws IOException, NoSuchAlgorithmException { MessageDigest md = MessageDigest.getInstance("SHA-256"); FileInputStream fis = new FileInputStream(file); byte[] byteArray = new byte[1024]; int bytesRead; while ((bytesRead = fis.read(byteArray)) != -1) { md.update(byteArray, 0, bytesRead); } byte[] digest = md.digest(); StringBuilder sb = new StringBuilder(); for (byte b : digest) { sb.append(String.format("%02x", b)); } fis.close(); return sb.toString(); } }
标签:java,String,参考,代码,fileHashes,File,directory,import From: https://www.cnblogs.com/xmyfsj/p/18635169