Java--判断两个文件是否相同

判断两个文件是否相同

利用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++;
			}
			
		}
	}
	
	
	
}






  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值