TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
- import java.io.*;
- import java.nio.MappedByteBuffer;
- import java.nio.channels.FileChannel;
- import java.util.zip.CRC32;
- public class Test{
- public static void main(String[] args){
- try { //对文件进行crc校验
- long begin = System.currentTimeMillis();
- FileInputStream in = new FileInputStream("code.py");//指定目标文件
- FileChannel channel = in.getChannel(); //从文件中获取一个通道
- CRC32 crc = new CRC32();
- int length = (int)channel.size();
- ////用只读模式从该通道获取字节缓冲,实现文件到内存的映射
- MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, length);
- for(int i = 0;i< length;i++)
- {
- int c = buffer.get(i);
- crc.update(c);//按字节做crc
- }
- System.out.println("crc校验和:"+(Long.toHexString(crc.getValue())).toUpperCase());
- long end = System.currentTimeMillis();
- System.out.println("运行"+(end-begin)+"ms");
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
复制代码
源码下载:http://file.javaxxz.com/2014/11/6/235908484.zip |
|