摘自:http://blog.csdn.net/yuyue618/article/details/8722887
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;
public class FileToCRCUtil {
public static String getCRC32(String fileUri) {
CRC32 crc32 = new CRC32();
FileInputStream fileinputstream = null;
CheckedInputStream checkedinputstream = null;
String crc = null;
try {
fileinputstream = new FileInputStream(new File(fileUri));
checkedinputstream = new CheckedInputStream(fileinputstream, crc32);
while (checkedinputstream.read() != -1) {
}
crc = Long.toHexString(crc32.getValue()).toUpperCase();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileinputstream != null) {
try {
fileinputstream.close();
} catch (IOException e2) {
e2.printStackTrace();
}
}
if (checkedinputstream != null) {
try {
checkedinputstream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return crc;
}
public static void main(String[] args) {
String uri = "D:\\ETFMY100PHFUNDBulletin20130325.txt";
System.out.println(getCRC32(uri));
}
}
本文介绍了一个用于计算文件CRC32校验值的Java工具类。该工具通过FileInputStream和CheckedInputStream读取文件,并使用CRC32类计算文件的CRC32校验和。文章还提供了一个示例,展示了如何使用此工具类来获取指定文件的CRC32值。

1万+

被折叠的 条评论
为什么被折叠?



