Compare Two Different Files Line by Line in Java


In this article, we will compare two different text files which are saved in our system. We will check line by line of each text file, by comparing those we can identify the similarities and differences.

Let’s see how we can do it by using the Java programming language.

To Show You Some Instances

Instance-1

Below figure depict two different text files with same content and hence output will be two files with same content.

Instance-2

Below represents two files, say file1.txt and file2.txt with their content.

file1.txt

This is amazing.
Java Language.

file2.txt

This is amazing.
Python Language.

Here, we can notice both the files have different content at line-2. As file1, line-2 contains ‘Java Language’ and file2, line-2 contains ‘Python Language’

Algorithm

  • Step 1 − Create reader1 and reader2 as two BufferedReader objects and use them to read the two input text files line by line.

  • Step 2 − Create two variables. First, create a boolean variable called "areEqual" and initialise it to true. Second, create an int variable called "lineNum" and initialise it to 1. areEqual is a flag variable that is initially set to true and is changed to false when the input files' contents differ. The number of lines will be held in lineNum.

  • Step 3 − Read the contents of File 1 into Line 1 and File 2 into Line 2.

  • Step 4 − Continue reading the lines from files file1 and file2 into line1 and line2, respectively, until both files have been read through. Set "areEqual" to false if either line1 or line2 is null.

  • Step 5 − Declare that the content of both files is identical if areEqual is true. If the value of 'areEqual' is false, declare that the contents of the files are distinct.

  • Step 6 − Close the resources.

Multiple Approaches

We have provided the solution in different approaches.

  • By Using BufferedReader Class

  • By Using Memory Mapped File

Let’s see the program along with its output one by one.

Approach-1: By Using BufferedReader Class

Example

In this approach you will create objects of BufferedReader class and by using inbuilt readLine() method you will read content of both files and compare.

import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class Main{ public static void main(String[] args) throws IOException{ BufferedReader reader1 = new BufferedReader(new FileReader("E:\file1.txt")); BufferedReader reader2 = new BufferedReader(new FileReader("E:\file2.txt")); String line1 = reader1.readLine(); String line2 = reader2.readLine(); int lineNum = 1; boolean areEqual = true; 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("Both the files have same content"); } else { System.out.println("Both the files have different content"); System.out.println("In both files, there is a difference at line number: "+lineNum); System.out.println("One file has "+line1+" and another file has "+line2+" at line "+lineNum); } reader1.close(); reader2.close(); } }

Output

Both the files have different content
In both files, there is a difference at line number: 2
One file has Java Language. and another file has Python Language. at line 2

Note − Here the input scenario was like instance-2 which is explained above.

Approach-2: By Using Memory Mapped Files

Example

In this approach, we will make use of the memory-mapped file concept which is a kernel object that maps bytes from a disk file to the system's memory address and by manipulating the contents of the memory-mapped files we can get to know whether the contents are same or different.

import java.io.File; import java.nio.file.Path; import java.nio.file.Paths; import java.io.IOException; import java.io.RandomAccessFile; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; public class Main { public static void main(String[] args) { Path path1 = Paths.get("E://file1.txt"); Path path2 = Paths.get("E://file2.txt"); compare(path1,path2); } public static void compare(Path path1, Path path2) { try { RandomAccessFile randomAccessFile1 = new RandomAccessFile(path1.toFile(), "r"); RandomAccessFile randomAccessFile2 = new RandomAccessFile(path2.toFile(), "r"); FileChannel ch1 = randomAccessFile1.getChannel(); FileChannel ch2 = randomAccessFile2.getChannel(); if (ch1.size() != ch2.size()) { System.out.println("Both files have different content"); } long size = ch1.size(); MappedByteBuffer m1 = ch1.map(FileChannel.MapMode.READ_ONLY, 0L, size); MappedByteBuffer m2 = ch2.map(FileChannel.MapMode.READ_ONLY, 0L, size); if (m1.equals(m2)) { System.out.println("Both files have same content"); } } catch(Exception e){ System.out.println(e); } } }

Output

Both files have same content

Note − Here both the files which we have considered, they are having the same content.

In this article, we explored how to compare content of two different text files line by line in Java.

Updated on: 11-Jan-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements