Some times we need to calculte checksum of specified file. In java, We can use MessageDigest class to get checksum of specified file.
Sample Java Code
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class CheckSum {
public static void main(String args[]){
StringBuffer checksum;
try {
File file = new File("C:\\Test.zip");
FileInputStream is = new FileInputStream(file);
byte buffer[] = new byte[(int)file.length()];
is.read(buffer);
MessageDigest md = MessageDigest.getInstance("MD5");
md.reset();
md.update(buffer);
byte digest[] = md.digest();
checksum = new StringBuffer();
for(int i = 0; i <>
{
String digit = Integer.toHexString(0xff & digest[i]);
if(digit.length() == 1)
checksum.append('0');
checksum.append(digit);
}
System.out.println("checksum::"+checksum.toString());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Welcome to Java Junction. Java junction talks about latest Java & related Web Technology.
Subscribe to:
Post Comments (Atom)
ORM Framework for Kotlin
In Kotlin, ORM (Object-Relational Mapping) libraries provide a convenient way to interact with databases using object-oriented programming p...
-
Feature Comparison Feature Liferay(5.1+) JBoss Portal(2.7) Out of the box Tools Has rich set of out of the box portlets Not too muc...
-
OSGi technology is Universal Middleware. OSGi technology provides a service-oriented, component-based environment for developers and offers...
No comments:
Post a Comment