-5

I am new in Java, I have written code to compare two files of data in Java but that is only working for 1 line not for all lines or whole file. Below is my code.

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class CompareTextFiles {

    public static void main(String[] args) throws IOException {
        BufferedReader reader1 = new BufferedReader(new FileReader("D:/Data/file1.docx"));
        BufferedReader reader2 = new BufferedReader(new FileReader("D:/Data/file2.docx"));
        String line1 = reader1.readLine();
        String line2 = reader2.readLine();
        boolean areEqual = true;
        int lineNum = 1;
        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();
            lineNum++;
        }
        if (areEqual) {
            System.out.println("Two files have same content.");
        } else {
            System.out.println("Two files have different content. They differ at line " + lineNum);
            System.out.println("File1 has " + line1 + " and File2 has " + line2 + " at line " + lineNum);
        }
        reader1.close();
        reader2.close();
    }
}
James Z
  • 12,209
  • 10
  • 24
  • 44
Ankur
  • 19
  • 7
  • 6
    Debug your application and find out why. – f1sh Mar 05 '18 at 16:48
  • You should probably be using streams (`FileInputStream`) and not readers (`BufferedReader`). Comparing the files directly in binary will be faster and also less likely to break if you actually need to compare binary files. – markspace Mar 05 '18 at 16:50
  • You are comparing two DOCX files and only the first line works. Does it means that only the first line is equal or it breaks after the first line? Add the error in case it breaks. And side note: DOCX are xml filesand even though the content may be equal on your editor there are a lot of differences on the attributes values within the file. So comparing it the way you want will most probably not work – Jorge Campos Mar 05 '18 at 16:51
  • Just a tip, comparing file content is much better by hashing files check: https://stackoverflow.com/questions/304268/getting-a-files-md5-checksum-in-java – someguy76 Mar 05 '18 at 16:51
  • @someguy76 that doesn't show *what* is different though – Federico klez Culloca Mar 05 '18 at 16:52
  • This works for two .txt files, at least. – achAmháin Mar 05 '18 at 17:06
  • 1
    @FedericoklezCulloca True, but the code in the post doesnt identify what is different either, it just reads the line and does a `==` to check if its equal. In this case, i suggested hashing. But i understand what you mean if he wants to know what is actually different – someguy76 Mar 06 '18 at 11:55
  • @someguy76 true, I stand corrected. – Federico klez Culloca Mar 06 '18 at 11:56

1 Answers1

0

Use fileInputStream then use two nested for loop O(n^2) i know, just for now. then use .hasNext() <-- boolean for your stream object and if true use .next() which checks line by line

Mohamed Abdou
  • 441
  • 3
  • 11