Comparing two text files line by line in Java
Comparing two text files line by line in Java

How to Compare Two Files in Java

Comparing two files is a common task in programming, often used to identify differences, synchronize data, or verify integrity. This article provides a comprehensive guide on How To Compare Two Files In Java, offering a step-by-step approach and a complete code example.

Comparing two text files line by line in JavaComparing two text files line by line in Java

Comparing Files Line by Line Using BufferedReader

One of the most efficient ways to compare two text files in Java is to read them line by line using the BufferedReader class. This method allows for efficient memory usage, especially when dealing with large files.

Step-by-Step Guide:

  1. Import Necessary Classes: Begin by importing the required classes: java.io.BufferedReader, java.io.FileReader, and java.io.IOException.

  2. Create BufferedReader Objects: Create two BufferedReader objects, one for each file, to read the files line by line. Replace "C:\file1.txt" and "C:\file2.txt" with the actual file paths.

    BufferedReader reader1 = new BufferedReader(new FileReader("C:\file1.txt"));
    BufferedReader reader2 = new BufferedReader(new FileReader("C:\file2.txt"));
  3. Initialize Variables: Initialize a boolean variable areEqual to true (assuming the files are initially equal) and an integer variable lineNum to 1 to track the current line number.

  4. Read Lines and Compare: Use a while loop to read lines from both files simultaneously until the end of either file is reached. In each iteration:

    • Read a line from each file using reader1.readLine() and reader2.readLine().
    • If either line is null (indicating the end of the file), set areEqual to false and break the loop.
    • If both lines are not null, compare them using line1.equalsIgnoreCase(line2) for case-insensitive comparison. If they are not equal, set areEqual to false and break the loop.
    • Increment lineNum.
  5. Display Results: After the loop finishes, check the value of areEqual.

    • If true, print “Two files have same content.”
    • If false, print “Two files have different content. They differ at line ” followed by the lineNum and the differing lines from each file.
  6. Close Resources: Close the BufferedReader objects using reader1.close() and reader2.close() to release system resources.

Complete Java Code Example:

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

public class CompareTextFiles {
    public static void main(String[] args) throws IOException {
        // ... (Code from Step 2 to Step 6 as described above) ... 
    }
}

Example Output Scenarios:

Scenario 1: Identical Files

If file1.txt and file2.txt have identical content, the output will be:

Two files have same content.

Scenario 2: Different Files

If file1.txt contains “apple” and file2.txt contains “orange” on the first line, the output will be:

Two files have different content. They differ at line 1
File1 has apple and File2 has orange at line 1

This detailed guide provides a clear and concise explanation of how to compare two files in Java using BufferedReader. Remember to handle potential IOExceptions appropriately in a production environment.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

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