Java对文件CRC32值计算
CRC 即循环冗余校验码(Cyclic Redundancy Check
):是数据通信领域中最常用的一种查错校验码,其特征是信息字段和校验字段的长度可以任意选定。循环冗余检查( CRC )是一种数据传输检错功能,对数据进行多项式计算,并将得到的结果附在帧的后面,接收设备也执行类似的算法,以保证数据传输的正确性和完整性。
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.zip.CRC32; import java.util.zip.CheckedInputStream; /** * * 开发公司:sojson.com
* 版权:sojson.com
* CRC生成 * * 区分 责任人 日期 说明
* 创建 周柏成 2016年4月10日* @author zhou-baicheng * @email json@sojson.com * @version 1.0,2016年4月10日 * */ public class CRCUtils { public static String loadCRC32(String filePath) { CRC32 crc32 = new CRC32(); FileInputStream inputStream = null; CheckedInputStream checkedinputstream = null; String crcStr = null; try { inputStream = new FileInputStream(new File(filePath)); checkedinputstream = new CheckedInputStream(inputStream, crc32); while (checkedinputstream.read() != -1) { } crcStr = Long.toHexString(crc32.getValue()).toUpperCase(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e2) { e2.printStackTrace(); } } if (checkedinputstream != null) { try { checkedinputstream.close(); } catch (IOException e) { e.printStackTrace(); } } } return crcStr; } public static void main(String[] args) { String path = "C:/Users/Administrator/Desktop/robots.txt"; System.out.println(loadCRC32(path)); } }
可以直接运行。
版权所属:SO JSON在线解析
原文地址:https://www.sojson.com/blog/113.html
转载时必须以链接形式注明原始出处及本声明。
如果本文对你有帮助,那么请你赞助我,让我更有激情的写下去,帮助更多的人。