Click here to Skip to main content
15,794,275 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have two very large text files (more than 1 GB each) and I need to compare them in Java to identify the differences. However, reading the entire file into memory and then comparing line by line is not an efficient solution due to memory constraints. What would be the best approach to compare these large files efficiently in Java? Is there any algorithm or library that I can use to achieve this?

What I have tried:

until I don't understand what should I do for this.
Updated 7-Apr-23 9:14am

1 solution

I don't know Java but appears to be similar to C#.

The File.Open does not read in all the data and if you use a StreamReader (C#, don't know the Java Equivalent) for the inputs you can read each input line by line compare and if the lines are different you can output the line number and contents to a third file or keep in memory.

If you are keeping in memory have a threshold and when the threshold is reached, process the data, clear the area and continue.

Not elegant but doable.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900