判断两个文件是否相同
利用md5判断两个文件是否相同:
例子:
根据不同路径下两个文件来判断:
package com.letv.test; import java.io.FileInputStream; import java.io.InputStream; import java.security.MessageDigest; public class CheckSameFile { public static char[] hexChar = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; public static void main(String[] args) { String path1 = "d:/test/abc.jpg"; String path2 = "d:/test/asd/abc.jpg"; String hash_path1 = null; String hash_path2 = null; try { hash_path1 = getHash(path1, "MD5"); hash_path2 = getHash(path2, "MD5"); } catch (Exception e) { e.printStackTrace(); } System.out.println("path1 md5:" + hash_path1); System.out.println("path2 md5:" + hash_path2); if (hash_path1.endsWith(hash_path2)) { System.out.println("文件相同"); } else { System.out.println("文件不相同"); } } /** * 获得文件md5值 */ public static String getHash(String fileName, String hashType) throws Exception { InputStream fis; fis = new FileInputStream(fileName); byte[] buffer = new byte[1024]; MessageDigest md5 = MessageDigest.getInstance(hashType); int numRead = 0; while ((numRead = fis.read(buffer)) > 0) { md5.update(buffer, 0, numRead); } fis.close(); return toHexString(md5.digest()); } /** * md5转成字符串 */ public static String toHexString(byte[] b) { StringBuilder sb = new StringBuilder(b.length * 2); for (int i = 0; i < b.length; i++) { sb.append(hexChar[(b[i] & 0xf0) >>> 4]); sb.append(hexChar[b[i] & 0x0f]); } return sb.toString(); } }
例子:
获取目录下所有所有文件,列出其中相同的文件,并打印结果:
package com.letv.test; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.security.MessageDigest; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; public class CheckSameFile { public static char[] hexChar = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; public static void main(String[] args) { Map<String,List<File>> filemap=new HashMap<String,List<File>>(); printInfo("d:/test", filemap);//修改路径判断对应路径下面的相同文件 } /** * 获得文件md5值 * @param fileName * @param hashType * @return * @throws Exception */ public static String getHash(String fileName, String hashType) throws Exception { InputStream fis; fis = new FileInputStream(fileName); byte[] buffer = new byte[1024]; MessageDigest md5 = MessageDigest.getInstance(hashType); int numRead = 0; while ((numRead = fis.read(buffer)) > 0) { md5.update(buffer, 0, numRead); } fis.close(); return toHexString(md5.digest()); } /** * md5转成字符串 * @param b * @return */ public static String toHexString(byte[] b) { StringBuilder sb = new StringBuilder(b.length * 2); for (int i = 0; i < b.length; i++) { sb.append(hexChar[(b[i] & 0xf0) >>> 4]); sb.append(hexChar[b[i] & 0x0f]); } return sb.toString(); } /** * 将所有的文件装入map * @param path * @return */ public static void getSameFile(String path,Map<String,List<File>> filemap){ File f= new File(path); if(f.isFile()){ String key; try { key = getHash(f.getPath(),"MD5"); if(filemap.containsKey(key)){ List<File> fd=filemap.get(key); fd.add(f); }else{ List<File> fs=new ArrayList<File>(); fs.add(f); System.out.println(f.getName()); filemap.put(key, fs); } } catch (Exception e) { e.printStackTrace(); } }else{ File[] fs=f.listFiles(); for(File f0:fs){ getSameFile(f0.getPath(), filemap); } } } /** * 打印判断结果 * @param path * @param filemap */ public static void printInfo(String path,Map<String,List<File>> filemap){ getSameFile(path,filemap); // System.out.println(filemap); Collection<List<File>> sets=filemap.values(); int i=1; for(List<File> f:sets){ // System.out.println(f.size()); if(f.size()>1){ System.out.println("-------------------"); System.out.println("第"+i+"组:以下"+f.size()+"文件内容相同:"); int j=1; for(File f_0:f){ System.out.println(j+" 文件名字:"+f_0.getName()+"---文件路径:"+f_0.getPath()+"\n"); j++; } System.out.println("-------------------"); i++; } } } }