Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need a program in java that compares two text input files, line by line, for e

ID: 3640192 • Letter: I

Question

I need a program in java that compares two text input files, line by line, for equality. Then prints any lines that are not equivalent.


Example: textfile1.txt line1 hello this is a test

line2 to see how two files

line3 can be compared

line4 to see how it works

textfile2.txt line1 hello this is a test

line2 to see how two files

line3 can be compared

line4 this is how it works


the user would see on the console: "the files are equal except for:" textfile1 line4 "to see how it works" textfile2 line4 "this is how it works"

Explanation / Answer

import java.io.*;
class FileRead
{
public static void main(String args[])
{
try{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("textfile.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
System.out.println (strLine);
}
//Close the input stream
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}
this will help you ....
you should use string compare to see if two lines are equal , if not you either print them right away , or copy to some other string that will keep all lines that are not equal and then prints them at the end , use for while loop , and create some count of lines so you know which line is not equal ...

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote