FileDocCategorySizeDatePackage
CheckSum.javaAPI DocExample1262Wed Jun 16 16:40:48 BST 2004None

CheckSum

public class CheckSum extends Object
CheckSum - print the checksum of a file
author
Ian F. Darwin, http://www.darwinsys.com/
version
$Id: CheckSum.java,v 1.6 2004/06/16 20:40:47 ian Exp $

Fields Summary
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] av)

        CheckSum c = new CheckSum();
		int sum = 0;
        if (av.length == 0)
            sum = c.process(new BufferedReader(
					new InputStreamReader(System.in)));
        else for (int i=0; i<av.length; i++)
			try {
				sum += c.process(new BufferedReader(new FileReader(av[i])));
			} catch (FileNotFoundException e) {
				System.err.println(e);
			}
		System.out.println(sum);
    
public intprocess(java.io.BufferedReader is)
CheckSum one text file, given an open BufferedReader. Checksumm does not include line endings, so will give the same value for given text on any platform. Do not use on binary files!

		int sum = 0;
        try {
            String inputLine;

            while ((inputLine = is.readLine()) != null) {
				int i;
				for (i=0; i<inputLine.length(); i++) {
					sum += inputLine.charAt(i);
				}
            }
            is.close();
        } catch (IOException e) {
            System.out.println("IOException: " + e);
		}
		return sum;