crc32 a File - Java Security

Java examples for Security:CRC

Description

crc32 a File

Demo Code


//package com.java2s;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.zip.CRC32;
import java.util.zip.CheckedInputStream;

public class Main {

    public static long fileCRC32(String fileName) {
        CheckedInputStream cis = null;
        try {//from  www  .ja v  a  2 s.  c o  m
            cis = new CheckedInputStream(new FileInputStream(fileName),
                    new CRC32());

            byte[] buf = new byte[128];
            while (cis.read(buf) >= 0)
                ;

            return cis.getChecksum().getValue();
        } catch (IOException e) {
            e.printStackTrace();
            return -1;
        } finally {
            try {
                if (cis != null) {
                    cis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

Related Tutorials