0

I am trying to develop a program that searches for duplicate files using MD5 hash, it will compare two hash files for duplicate.

I am having difficulties comparing the two files, after hashing the files with MD5 hash code, I keep getting the error "Java.IO.FileNotFoundException.". Here is my code, I do not know what I am doing wrong.

////////////////////// 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);
               }

               }
Bosko Mijin
  • 3,287
  • 3
  • 32
  • 45
emekamba
  • 188
  • 9
  • 1
    `java.io.FileNotFoundException` suggests that a file you're trying to read can't be found. Which file are you trying to read? Have you confirmed that it exists (and you have permissions to read it)? What does `new File(Arrays.toString(f1)).getAbsolutePath()` return? – yshavit Nov 12 '14 at 20:24
  • 2
    The lines where your problems are make _no_ sense. You're creating an array of files with size `i`, converting that empty array to a string, and then treating that as a new file!? – Louis Wasserman Nov 12 '14 at 20:24
  • @ yshavit I am trying to read the hashed value, and compare them for duplicates. – emekamba Nov 12 '14 at 20:46
  • Please try instantiating the File object just with a _correct_ pathname. Using the constructor described [here](https://docs.oracle.com/javase/7/docs/api/java/io/File.html#File(java.lang.String)) The point of @LouisWasserman is the key imo – gutenmorgenuhu Nov 13 '14 at 18:49

1 Answers1

0

For reading files in Java you need an InputStream object. Look at this Question Getting a File's MD5 Checksum in Java which seems to help you with your problem

Community
  • 1
  • 1
gutenmorgenuhu
  • 2,312
  • 1
  • 19
  • 33
  • But the problem I am facing is reading the files that has been hashed and comparing them for duplicates. will appreciates ur help. – emekamba Nov 13 '14 at 09:44