Creating a checksum of an InputStream

Use the class CheckedInputStream. This class is a FilterInputStream and calculates a checksum as data passes through it. When creating it, pass it an existing inputstream and an instance of a class that implements the Checksum interface. In the standard API, there are two such classes available: and
The checksum value can be retrieved by invoking getChecksum().getValue().

This method calculates the checksum of a file.

public class Main {
   public static void main(String args[]) throws Exception {
      if (args.length != 1) {
         System.err.println("Usage: java Main <file.txt>");
      File file = new File(args[0]);
      CheckedInputStream cis = new CheckedInputStream(new FileInputStream(file), 
                                                      new CRC32());
      byte []buffer = new byte[1024];
      while ( > 0) { }
      System.out.println("Checksum of file " + args[0] + " = " 
                            + cis.getChecksum().getValue());