File Difference Checker in Java: Explanation & Practice

Compare two files and show differences

Problem summary

Compare two text files line by line and report lines that differ.

Starter code

import java.io.*;

public class Main {
    public static void main(String[] args) {
        // Compare file1.txt and file2.txt
        // Print lines that differ
        // Format: Line <n>: <file1 line> != <file2 line>
    }
}

Expected output and test cases

  • Line 3 differs
    Line 3: Hello != World
  • No differences
    Files identical
  • Different line counts
    File lengths differ

Hints

  1. Read both files line by line
  2. Compare corresponding lines
  3. Handle different file lengths

Related File I/O exercises

Practice all File I/O exercises · Run this idea in the Java compiler