日期:2014-05-20 浏览次数:20754 次
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.zip.CRC32;
public class CRC32Test {
/**
* 测试用例
*/
public static void main(String[] args) throws Exception {
final String FileName = "D:\\tmp\\log.txt";
long crc = readCRC32(new File(FileName));
System.out.println(crc);
System.out.println(Long.toHexString(crc));
}
public static long readCRC32(File file) throws IOException{
CRC32 crc = new CRC32();
byte[] buffer = new byte[4*1024];
FileInputStream fis = new FileInputStream(file);
try{
int len = 0;
while((len=fis.read(buffer))>0){
crc.update(buffer, 0, len);
}
}finally{
fis.close();
}
return crc.getValue();
}
}