Get CRC32 checksum in Java
Description
The following code shows how to get CRC32 checksum.
Example
// ww w .jav a2s . co m
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.CRC32;
import java.util.zip.Checksum;
public class Main {
public static void main(String[] args) throws IOException {
FileInputStream fin = new FileInputStream("Main.java");
System.out.println(getCRC32(fin));
fin.close();
}
public static long getCRC32(InputStream in) throws IOException {
Checksum cs = new CRC32();
// more efficient to read chunks of data at a time
for (int b = in.read(); b != -1; b = in.read()) {
cs.update(b);
}
return cs.getValue();
}
}
The code above generates the following result.