The following list is used to compute the 32-bit cyclic redundancy checksum (CRC32) of a file that is often used to determine whether a file is corrupted, because file corruption can lead to checksum changes. Java.util.zip Bag
Contains a CRC32 class that can use loops to compute the checksum of a sequence of bytes.
Package cn.ls;
Import java.io.*;
Import java.nio.*;
Import java.nio.channels.*;
Import java.util.zip.*; /** * This program computes the CRC checksum of a file.
<br> * Usage:java niotest filename * @version 1.01 2004-05-11 * @author Cay Horstmann * * public class Niotest { public static long Checksuminputstream (String filename) throws IOException {InputStream in = new FILEINPUTST
Ream (filename);
CRC32 CRC = New CRC32 ();
int C;
while ((c = in.read ())!=-1) crc.update (c);
return Crc.getvalue (); public static long Checksumbufferedinputstream (String filename) throws IOException {InputStream in = new B
Ufferedinputstream (filename) (new FileInputStream);
CRC32 CRC = New CRC32 ();
int C;
while ((c = in.read ())!=-1) crc.update (c);
return Crc.getvalue (); public static long Checksumrandomaccessfile (String filename) throws IOException {Randomaccessfile file = n EW RandoMaccessfile (filename, "R");
Long length = File.length ();
CRC32 CRC = New CRC32 ();
for (long p = 0; p < length; p++) {File.seek (P);
int c = File.readbyte ();
Crc.update (c);
return Crc.getvalue (); public static long Checksummappedfile (String filename) throws IOException {FileInputStream in = new Filein
Putstream (filename);
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 p = 0; p < length; p++) {int c = Buffer.get (p);
Crc.update (c);
return Crc.getvalue ();
public static void Main (string[] args) throws IOException {System.out.println ("Input Stream:");
Long start = System.currenttimemillis ();
Long Crcvalue = Checksuminputstream ("f:/x.html"); Long end = System. Currenttimemillis ();
System.out.println (long.tohexstring (Crcvalue));
System.out.println ((End-start) + "milliseconds");
System.out.println ("Buffered Input Stream:");
Start = System.currenttimemillis ();
Crcvalue = Checksumbufferedinputstream ("f:/x.html");
End = System.currenttimemillis ();
System.out.println (long.tohexstring (Crcvalue));
System.out.println ((End-start) + "milliseconds");
System.out.println ("Random Access File:");
Start = System.currenttimemillis ();
Crcvalue = Checksumrandomaccessfile ("f:/x.html");
End = System.currenttimemillis ();
System.out.println (long.tohexstring (Crcvalue));
System.out.println ((End-start) + "milliseconds");
SYSTEM.OUT.PRINTLN ("Mapped File:");
Start = System.currenttimemillis ();
Crcvalue = Checksummappedfile ("f:/x.html");
End = System.currenttimemillis ();
System.out.println (long.tohexstring (Crcvalue)); System.out.println ((End-start) + "milliseconds");
}
}
The experimental results are as follows:
Input Stream:
599af5f2
2303 milliseconds
Buffered Input Stream:
599af5f2
Milliseconds
Random Access File:
599af5f2
3038 milliseconds
Mapped File:
599af5f2
Milliseconds
Memory mappings are faster than sequential reads with Buffering.