获取文件的MD5值,比较两个文件是否完全相同

代码:

public class MD5Test {
	public static void main(String[] args) {
		String s1 = MD5Test.MD5Operation(new File("E:/a.csv"));
		String s2 = MD5Test.MD5Operation(new File("E:/data/a.csv"));
		System.out.println(s1.equals(s2));
	}
	    
    public final static String MD5Operation(File file) {
		try {			
			MessageDigest md = MessageDigest.getInstance("MD5");
			//采用commons-io包的FileUt类的方法,极大地简化了代码。
			byte temp[] = FileUtils.readFileToByteArray(file);
			md.update(temp);
			byte b[] = md.digest();
			//采用java.math包的BigInteger类,实现十六进制的转换
			BigInteger bigInt = new BigInteger(1, b);
			return bigInt.toString(16);
		} catch (Exception e) {
			return null;
		}      
    }
}
posted @ 2014-07-15 16:22  小样儿1020  阅读(1015)  评论(0编辑  收藏  举报