[SOLVED] Java – Comparing two huge text files

Issue

I am trying to develop a basic java program to compare two huge text files and print non matching records .i.e. similar to minus function in SQL. but I am not getting the expected results because all the records are getting printed even though both files are same. Also suggest me whether this approach is performance efficient for comparing two huge text files.

import java.io.*;

public class CompareTwoFiles {
    static int count1 = 0 ;
    static int count2 = 0 ;

    static String arrayLines1[] = new String[countLines("\\Files_Comparison\\File1.txt")];
    static String arrayLines2[] = new String[countLines("\\Files_Comparison\\File2.txt")];

    public static void main(String args[]){  
        findDifference("\\Files_Comparison\\File1.txt","\\Files_Comparison\\File2.txt");
        displayRecords();
    }

    public static int countLines(String File){

        int lineCount = 0;
        try {
           BufferedReader br = new BufferedReader(new FileReader(File));
           while ((br.readLine()) != null) {
               lineCount++;
           }

       } catch (FileNotFoundException e) {
           e.printStackTrace();
       } catch (IOException e) {
           e.printStackTrace();
       }
           return lineCount;
    }

    public static void findDifference(String File1, String File2){
        String contents1 = null;  
        String contents2 = null; 
        try  
        {  
            FileReader file1 = new FileReader(File1);  
            FileReader file2 = new FileReader(File2);
            BufferedReader buf1 = new BufferedReader(file1); 
            BufferedReader buf2 = new BufferedReader(file2);

           while ((contents1 = buf1.readLine()) != null)  
            {  
               arrayLines1[count1] = contents1 ;
               count1++;
            }  

           while ((contents2 = buf2.readLine()) != null)  
            {  
               arrayLines2[count2] = contents2 ;
               count2++;
            }
       }catch (Exception e){
           e.printStackTrace();
       }
}



    public static void displayRecords() {      
        for (int i = 0 ; i < arrayLines1.length ; i++) {    
            String a = arrayLines1[i];  
            for (int j = 0; j < arrayLines2.length; j++){  
                String b = arrayLines2[j];  
                boolean result = a.contains(b);  
                   if(result == false){  
                       System.out.println(a);  
                   }  
            }

        }
    }
}

Solution

Based upon your explanation you do not need embedded loops

consider

public static void displayRecords() { 

    for (int i = 0 ; i < arrayLines1.length && i < arrayLines2.length; i++) 
    {    
        String a = arrayLines1[i];  
        String b = arrayLines2[i];  

        if(!a.contains(b){  
               System.out.println(a);  
        }  
    }

Answered By – Scary Wombat

Answer Checked By – Willingham (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *