FileReader / BufferedReader Help
I'm trying to write a program to compare two text files and write the
results to a third file. If the compared text files are different the
results file contains the line number where the difference was found and the
corresponding lines from each of the files being compared.
I originally thought I could use FileReader / BufferedReader to read lines
of text from each file and then compare the strings using the equals method.
On testing the program I discovered that some lines incorrectly passed the
comparison. For instance, a line of text terminated with crlf (newline) was
determined equal to the same line of text without the crlf termination.
Here's a code snippet:
FileReader inputFile1 = new FileReader(args[0]);
FileReader inputFile2 = new FileReader(args[1]);
BufferedReader bufferInputFile1 = new BufferedReader(inputFile1);
BufferedReader bufferInputFile2 = new BufferedReader(inputFile2);
String lineInFile1 = bufferInputFile1.readLine();
String lineInFile2 = bufferInputFile2.readLine();
int lineCount++;
while ((lineInFile1 != null)&&(lineInFile2 != null)) {
if (!lineInFile1.equals(lineInFile2)){
bufferOutputFile.write("Line " + lineCount + " of compare source file
does not match compare base file");
bufferOutputFile.newLine();
bufferOutputFile.write("Source: ");
bufferOutputFile.write(lineInFile1.trim());
bufferOutputFile.newLine();
bufferOutputFile.write("Base: ");
bufferOutputFile.write(lineInFile2.trim());
bufferOutputFile.newLine();
bufferOutputFile.newLine();
result = false;
}
lineInFile2 = bufferInputFile2.readLine();
lineInFile1 = bufferInputFile1.readLine();
lineCount++;
}
I'm not a developer by trade and would be appreciative if someone more
knowledgeable could briefly explain why this approach isn't working. If it's
not too much trouble, suggestions/recommendations on an alternate approach
would be great.
Thank you.