This code snippet shows how to CRC file in Java. The CRC32 algorithm generates 32 bit hash value of a file. The exact same code snippet that
was posted in C# section is modified little for Java developers.

Code:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;


public class crc
{

    public static final int Polynomial = 0x04c11db7;
    public static final String fname = "c:\\myfiles\\file1.exe";
    public static long fsize = 0; 
    public static int crc32_table[] = new int[256];  
    
    

    
    //####################### main ###################
    public static void main(String[] args)
    {
        InitCRCTable();
        System.out.print("CRC: " + Integer.toHexString(CreateCRC()));
        return;

    }
    
    
    //###############################################################
    //# Creates CRC and returns it to the caller 
    //###############################################################

    public static int CreateCRC()
    {

       int crc = 0xFFFFFFFF;
    
                       
      try  
      {
      
       
       FileInputStream fin = new FileInputStream(fname); 
     
       File f = new File(fname);
       fsize = f.length();            // get the file size
          
          
       byte buffer[] = new byte[(int)fsize];
    

       fin.read(buffer); 
       fin.close();
             
        for(int  i = 0; i < fsize; i++)
               crc = (crc >>> 8) ^ crc32_table[(crc & 0xFF) ^ (buffer[i] & 0xFF)];            
              
      } // end try
      
      catch(FileNotFoundException e)
      {
        System.out.println(e.getMessage());    
        return 0;            
      }        
      catch(IOException e )
      {
        System.out.println(e.getMessage());    
        return 0;            
       }
      
      
      return  (crc ^ 0xFFFFFFFF);
          
        
    }

    //#######################################################################
    //# Reflect used by Initialize CRC function
    //# This function does swap with bits
    //#######################################################################

    public static int Reflect(int ref, byte ch)
    {       

        int value = 0;
    

        // Swap bit 0 for bit 7
        // bit 1 for bit 6, etc.

        for(int i = 1; i < (ch + 1); i++)
        {
            if((ref & 1) == 0)
            {/* do noting */ }
            else
                value |= 1 << (ch - i);
            
            ref >>>= 1;
        }
        
        return value;
        
    }

    //#######################################################################
    //# Initialize the crc table
    //#######################################################################

    public static void InitCRCTable()
    {

        // 256 values representing ASCII character codes.
            

        for(int i = 0; i <= 0xFF; i++)
        {
            crc32_table[i] = Reflect(i, (byte)8) << 24;
            
            for (int j = 0; j < 8; j++)
                crc32_table[i] = (crc32_table[i] << 1) ^ (((crc32_table[i] & (1 << 31)) == 0) ? 0 : Polynomial);
            
             
            crc32_table[i] = Reflect(crc32_table[i], (byte)32);    
            
        }
    }
}