Java File Read Line by Line and Compare
Problem :
Write a java program to compare two text files line past line. Your program should take 2 text files as input and compare their content line by line and notice out whether they have aforementioned content or not.
How To Compare 2 Text files Line By Line In Java?
Step 1 : Define twoBufferedReader objects,reader1 andreader2, to read the both input text files line past line.
BufferedReader reader1 = new BufferedReader(new FileReader("Pass the path of file1 here"))
BufferedReader reader2 = new BufferedReader(new FileReader("Laissez passer the path of file2 here"))
Footstep ii : InitializeareEqual with true andlineNum with 1.areEqualvolition be a flag variable which will be initially set to true and it volition be set to simulated when the content of input files differ.lineNumvolition hold the count of number of lines.
boolean areEqual = true;
int lineNum = 1;
Step 3 : Read the lines of file1 into line1 and lines of file2 into line2.
String line1 = reader1.readLine()
String line2 = reader2.readLine()
Pace 4 : Keep reading the lines of file1 into line1 and lines of file2 into line2 till the end of the files. If any i of line1 orline2 is aught, then assign simulated to areEqual and pause the loop. If both, line1 and line2, are non nada then compare them usingequalsIgnoreCase() method. If information technology returns true and then continue with the loop. Otherwise intermission the loop and assignfaux to areEqual.
while (line1 != naught || line2 != null)
{
if(line1 == nada || line2 == null)
{
areEqual = false;
break;
}
else if(! line1.equalsIgnoreCase(line2))
{
areEqual = false;
break;
}
line1 = reader1.readLine();
line2 = reader2.readLine();
lineNum++;
}
Step 5 : IfareEqualis true so declare both files have aforementioned content. IfareEqualis imitation then declare both files have different content.
Stride 6 : Close the resources.
Java Program To Compare Two Text Files Line By Line :
import coffee.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public form CompareTextFiles { public static void chief(String[] args) throws IOException { BufferedReader reader1 = new BufferedReader(new FileReader("C:\\file1.txt")); BufferedReader reader2 = new BufferedReader(new FileReader("C:\\file2.txt")); String line1 = reader1.readLine(); String line2 = reader2.readLine(); boolean areEqual = true; int lineNum = 1; while (line1 != null || line2 != nada) { if(line1 == null || line2 == nix) { areEqual = false; break; } else if(! line1.equalsIgnoreCase(line2)) { areEqual = fake; break; } line1 = reader1.readLine(); line2 = reader2.readLine(); lineNum++; } if(areEqual) { Arrangement.out.println("2 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(); } }
Output ane :
file1 :
Abhi 71
Bhavani 68
Mahesh 89
Nalini 62
Shloka 84
Kaya 84
Siya 56
Vikas 92
file2 :
Abhi 71
Bhavani 68
Mahesh 89
Nalini 62
Shloka 84
Kaya 84
Siya 56
Vikas 92
Result :
Two files have same content.
Output 2 :
file1 :
Abhi 71
Bhavani 68
Mahesh 89
Vikas 92
Shloka 84
Shyam 81
Suresh 56
Nalini 62
file2 :
Abhi 71
Bhavani 68
Mahesh 89
Nalini 62
Shloka 84
Kaya 84
Siya 56
Vikas 92
Result :
Two files take different content. They differ at line 4
File1 has Vikas 92 and File2 has Nalini 62 at line 4
Also Read :
How to sort text files in java?
How to append text to a file in coffee?
How to set file permissions in coffee?
How to listing all files in a directory in java?
Source: https://javaconceptoftheday.com/compare-two-text-files-in-java/
0 Response to "Java File Read Line by Line and Compare"
Postar um comentário