0
\$\begingroup\$

Program to compare if files within two directories are having same content I followed the approach of getting the list of files under two directories recursively and store it in different list with it is canonical path.

Then using two loops, compared the each file line by line. Below is my code,

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class FileCompare
{
    static List<String> dir1_filePath = new ArrayList<String>();
    static List<String> dir2_filePath = new ArrayList<String>();
    static void fetchFiles(File[] arr,int start,int level, List<String> filePath) throws IOException 
    {
        if(start == arr.length)
            return;
        if(arr[start].isFile()){
            filePath.add(arr[start].getCanonicalPath());
        }
        else if(arr[start].isDirectory())
        {
            fetchFiles(arr[start].listFiles(), 0, level + 1,filePath);
        }
        fetchFiles(arr,++start, level,filePath);
    }

    public static void main(String[] args) throws IOException
    {
        Scanner s = new Scanner(System.in);
        String directory_one = s.next();
        String directory_two = s.next();
        File dir_one = new File(directory_one);
        File dir_two = new File(directory_two);

        if(dir_one.exists() && dir_one.isDirectory())
        {
            File dir1_files[] = dir_one.listFiles();    
            fetchFiles(dir1_files,0,0, dir1_filePath); 
        }
        if(dir_two.exists() && dir_two.isDirectory())
        {
            File dir2_files[] = dir_two.listFiles();    
            fetchFiles(dir2_files,0,0, dir2_filePath); 
        } 
        boolean fileEqual;
        for(int i=0;i<dir1_filePath.size();i++){
            for(int j=1; j<dir2_filePath.size();j++){
                fileEqual = compareTwoFiles(dir1_filePath.get(i), dir2_filePath.get(j));
                if(fileEqual){
                    System.out.println(dir1_filePath.get(i) + " and " + dir2_filePath.get(i) + " are Equal");
                }
            }
        }
    }

    private static boolean compareTwoFiles(String file1, String file2) throws IOException {
        BufferedReader reader1 = new BufferedReader(new FileReader(file1));        
        BufferedReader reader2 = new BufferedReader(new FileReader(file2));
        String line1 = reader1.readLine();         
        String line2 = reader2.readLine();
        boolean areEqual = true;
        while (line1 != null || line2 != null)
        {
            if(line1 == null || line2 == null)
            {
                areEqual = false;
                break;
            }
            else if(! line1.equalsIgnoreCase(line2))
            {
                areEqual = false;
                break;
            }
            line1 = reader1.readLine();
            line2 = reader2.readLine();
        }     
        reader1.close();     
        reader2.close();
        return areEqual;
    }
}

Is there any other approach to compare the files effectively, instead of using two loops?

\$\endgroup\$
  • \$\begingroup\$ Are you trying to compare every file in directory 1 with every single file in directory 2, thus making N x M comparisons? Or are you meant to only compare files with the same name and in the same subdirectory? \$\endgroup\$ – DodgyCodeException Mar 19 '18 at 18:04
  • \$\begingroup\$ How equal? if absolutely equal, then the first check would be file size. \$\endgroup\$ – AJD Mar 19 '18 at 18:54
  • 1
    \$\begingroup\$ You could also create a hash of the case insensitive contents and compare the hashes. \$\endgroup\$ – AJD Mar 19 '18 at 18:56
  • \$\begingroup\$ @DodgyCodeException, Yes I want to compare the same content in files from two different directories. Not comparing with the name, with the content of the files from different directories.(Even in sub directory) \$\endgroup\$ – irs102info Mar 20 '18 at 3:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.