NL1
NL1

Reputation: 17

File comparison and summaries in Java

I am new in java and written code to compare 2 files which contains 3-5 pages. If there is any difference in any file for any line, then it produces the location/line where the difference found.
However my code is comparing only 1 line and not all the lines and pages.

When I execute the code I'm getting output in an unreadable format:

Two files have different content. They differ at line 6
File1 has ð¼Ñêz£¿§Å‰…, ¡ ‰/û|f\ZþçŠæ?6ï!Y´_áoœ]Aó

The 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("v1.docx"));
BufferedReader reader2 = new BufferedReader(new FileReader("v2.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();
}
}

Upvotes: 0

Views: 72

Answers (1)

Klevin Delimeta
Klevin Delimeta

Reputation: 168

When i am executing code gettitng outout in unreadalbe format

You are reading from a .docx file. It's like this:

Try opening your v1.docx file with a notepad.exe or wordpad.exe, and look what weird characters you will see.

You should find a library which support reading .docx file types.

Upvotes: 1

Related Questions