我正在尝试开发一个使用MD5哈希搜索重复文件的程序,它会比较两个哈希文件的重复。
我在比较两个文件时遇到困难,在使用MD5哈希码对文件进行哈希处理后,我不断收到错误“Java.IO.FileNotFoundException。”。这是我的代码,我不知道我做错了什么。
////////////////////// It is a GUI Program ////////////////////////
DefaultListModel m = new DefaultListModel(); // List Model for displaying the hash codes
int rval = chooser.showOpenDialog(null); //JFileChooser for selecting files tobehashed
if(rval == JFileChooser.APPROVE_OPTION){
File f = chooser.getCurrentDirectory();
String fs = f + "";
if(!f.isDirectory()){
JOptionPane.showMessageDialog(null, "Supplied Directory does not exist");
}
//display files on the TesxtField component
File[] filenames = f.listFiles();
String fn = Arrays.toString(filenames);
String type = f.isFile() ? "File" : "Directory : ";
long len = f.length();
String all = type +" "+" " + " Length: " + len;
dis.setText(all + "\n");
dis.setText(fn + "\n" + "\n" );
//Loops through the file and check sum of the list files
for(File file : f.listFiles()){
String hash;
try {
hash = MD5.asHex(MD5.getHash(file));
////////// Here is where my problems starts, Please help //////////
for(int i = 0; i < hash.length(); i++ )
for(int j = i + 1; j < hash.length(); j++){
File[] f1 = new File[i];
File[] f2 = new File[j];
boolean check = MD5.hashesEqual(MD5.getHash(new File(Arrays.toString(f1))),MD5.getHash(new File(Arrays.toString(f2)))); //compares the byte of files
System.out.println(check);
m.addElement(hash);
task.setModel(m);
}
}catch (IOException ex) {
JOptionPane.showMessageDialog(null, ex);
}
}
答案 0 :(得分:0)
要读取Java中的文件,您需要一个InputStream对象。 看看这个问题Getting a File's MD5 Checksum in Java,这似乎可以帮助您解决问题