簡體   English   中英

比較Java中的兩個字節數組

[英]Comparing two byte arrays in java

我正在嘗試開發一個使用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);
               }

               }

為了使用Java讀取文件,您需要一個InputStream對象。 看看這個問題,用Java獲取文件的MD5校驗和似乎可以幫助您解決問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM