0

I have this code that tell me if the 2 files have the same content. However, I want to expand this to not just one file but to directory Files. In other words, compare files in one folder to files in another to see if they have same or different contents and if so print it out.

In doing this is it just a matter of modifying the directory paths? My code is below. I think I remember reading somewhere that I may need import another library. Any help would be appreciated.

Problem now I am having is printing out the file name from the area that are the same and that are not the same I tried .getName it is not working my result come back (true) (instead of the names of the files that meet the condition

Code below

import java.io.File;

public class F2FCompare {

 public static void main(String[] args) 
    {    
     File folderA = new File("C:/Users/hoflerj/Desktop/FolderA");
     File[] listOfFilesInA = folderA.listFiles();

     File folderB = new File("/C:/Users/hoflerj/Desktop/FolderB");
     File[] listOfFilesInB = folderB.listFiles();   

     for (File fileA : listOfFilesInA){
         if (fileA.isFile()) 
         {
             for (File fileB : listOfFilesInB) {
                  if(fileB.isFile()) 
                    {
                      if ( fileA.equals(fileB)) 
                      {
                          System.out.println( "FolderA same as FolderB" + listOfFilesInA.equals(listOfFilesInB) );
                          //File[] files = folder.listFiles(); 
                      }

                     // txtfile.getName()
                      else  
                      {
                          //System.out.println(! fileA.getName().equals(fileB) + "    Folder A diff B" );
                          System.out.println( "Folder same as FolderB  " + !fileA.getName().equals(fileB) );
                      }



                    }
                } 
            }
        }
    }
}
Jonathan
  • 395
  • 2
  • 8
  • 25
  • First I suggest you put this code into a method. Then figure out an algorithm to solve your problem. To do this, turn off your computer and get a piece of paper and pencil. Write the steps needed to solve the problem **in words**. Do not worry overly much about Java syntax. – Code-Apprentice Jul 05 '17 at 19:32
  • Possible duplicate of [Find files in a folder using Java](https://stackoverflow.com/questions/4852531/find-files-in-a-folder-using-java) – K.Nicholas Jul 05 '17 at 19:36
  • Possible duplicate of [Printing Out File Names that are Different](https://stackoverflow.com/questions/44932665/printing-out-file-names-that-are-different) – Abhijit Sarkar Jul 05 '17 at 21:29

2 Answers2

1

Try changing:

if(line1 == null || line2 == null)

for:

if((line1 == null && line2 != null) || (line2 == null && line1 != null))
Fabien
  • 4,862
  • 2
  • 19
  • 33
1

This code will iterate over every file in folder A and compare it to every file in folder B.

File folderA = new File("/Users/you/folderA/");
File[] listOfFilesInA = folderA.listFiles();
File folderB = new File("/Users/you/folderB/");
File[] listOfFilesInB = folderB.listFiles();    
for (File fileA : listOfFilesInA) {
    if (fileA.isFile()) 
    {
        for (File fileB : listOfFilesInB) {
             if(fileB.isFile()) 
             {
                //your code 
             }
         } 
    }
}

Credits to David Robles: https://stackoverflow.com/a/1844695/4788664

TinkeringMatt
  • 181
  • 1
  • 9
  • I believe this will work. I know I have to add my code to compare my logic but eclipse puts a red line under the word **file** in this statement ( if (file.isFile()) ). Should this be the word fileA? – Jonathan Jul 05 '17 at 19:58
  • yes exactyl my bad. I wrote the code in the snippet ! – TinkeringMatt Jul 05 '17 at 20:03
  • I reposted my new code above I believe I am on the right track however I get message in eclipse saying the following: Exception in thread "main" java.lang.NullPointerException at File2FileCompare.main(File2FileCompare.java:16) however I dont see any error on line 16 – Jonathan Jul 05 '17 at 20:49
  • 1 sec checking on something I think i might know what the issue is – Jonathan Jul 05 '17 at 20:50
  • I got it working it was my fault the paths were not write thats what I get for looking at the PC screen for hours. – Jonathan Jul 05 '17 at 20:51
  • Now my problem is that it defaults to the else if statement when the files in both folders are the same ( any help would be appreciated ) – Jonathan Jul 05 '17 at 20:57
  • try : else if ( ! fileA.equals(fileB)) – TinkeringMatt Jul 06 '17 at 12:08
  • in fact, here else if is not even necessary, you could only use else alone. Else if is used to test additionnal conditions. – TinkeringMatt Jul 07 '17 at 13:53
  • Now I am trying to do a count of all the files that are the same – Jonathan Jul 07 '17 at 19:04