JAVA IO - 生成文件的checksum值

import java.io.FileInputStream;
import java.security.MessageDigest;

public class CheckSumGenerator {
	public static void main(String args[]) throws Exception {
		String datafile = "C:\\work\\hello\\helloworld.txt";

		MessageDigest md = MessageDigest.getInstance("MD5");
		FileInputStream fis = new FileInputStream(datafile);
		byte[] dataBytes = new byte[1024];

		int nread = 0;

		while ((nread = fis.read(dataBytes)) != -1) {
			md.update(dataBytes, 0, nread);
		}
		;

		byte[] mdbytes = md.digest();

		// convert the byte to hex format
		StringBuffer sb = new StringBuffer("");
		for (int i = 0; i < mdbytes.length; i++) {
			sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16)
					.substring(1));
		}

		System.out.println("Digest(in hex format):: " + sb.toString());

	}
}



你可能感兴趣的:(MessageDigest)