MD5判断文件相同
程序员文章站
2024-03-19 10:51:34
...
commons-codec-1.11.jar
package demo;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.MessageDigest;
import javax.swing.JFileChooser;
import org.apache.commons.codec.digest.DigestUtils;
public class demo {
public static void main(String[] args) {
// TODO Auto-generated method stub
String path1 =null;
String path =null;
for(int i = 0;i<2;i++){
JFileChooser jf = new JFileChooser();
int b = jf.showOpenDialog(null);
if (b == JFileChooser.APPROVE_OPTION) {
File file = jf.getSelectedFile();
if(i==0){
path1=file.getPath();
}
else
{
path=file.getPath();
}
}
}
FileInputStream fis;
FileInputStream fis2;
try {
fis = new FileInputStream(path1);
fis2 = new FileInputStream(path);
System.out.println(path);
System.out.println(path1);
try {
String a = DigestUtils.md5Hex(fis);
String a1 = DigestUtils.md5Hex(fis2);
System.out.println(a);
System.out.println(a1);
if(a.equals(a1)){
System.out.println("他们文件相同");
}
else{
System.out.println("他们文件不相同");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}