Java Program ---------------------------------- FILE 2 Write a program that comp
ID: 3670429 • Letter: J
Question
Java Program
----------------------------------
FILE 2
Write a program that compares two text files for equality. Print out any lines that are different along with the line number Include the following: File I/O Exception Handling using a Try/Catch Block Algorithm: Initial and Refined · Internal Documentation ·All prologue information The two external files Use the following two files: File1.txt and File2.txt. These files are included. I have also attached zip files containing them because sometimes the files are converted to HTML files when downloaded,Explanation / Answer
/**The java program Equality that read two text files file1.txt and file2.txt and search for the line numbers from the two files that are not equal then print the line number and lines to console.*/
//Equality.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Equality
{
public static void main(String[] args)
{
//Set file names
String file1="file1.txt";
String file2="file2.txt";
//Create a Scanner class variables
Scanner file1scanner=null;
Scanner file2scanner=null;
//Set lineNum=1
int lineNum=1;
//File exception handling
//Throws an exception if file not found
try
{
//Create two scanner class objects to read input file file1 and file2
file1scanner=new Scanner(new File(file1));
file2scanner=new Scanner(new File(file2));
//read files until end of the file
while(file1scanner.hasNextLine() || file2scanner.hasNextLine())
{
//Get lines from the two file scanners
String linefile1=file1scanner.nextLine();
String linefile2=file2scanner.nextLine();
//Check if the linefile1 and linefile2 are not equal
if(!linefile1.equals(linefile2))
{
//print number line number
System.out.println("Line : "+lineNum);
//print lines
System.out.println(linefile1);
System.out.println(linefile2);
}
//increment the line number
lineNum++;
}
file1scanner.close();
file2scanner.close();
}
//Catch the exception
catch (FileNotFoundException e)
{
System.out.println(e.getMessage());
}
}
}//end of the class Equality
------------------------------- ------------------------------- -------------------------------
Sample intput text files
file1.txt
<HTML>
<Head>
<Title> Fred's Home Page </Title>
</Head>
<Body bgcolor = "white">
<Center>
<H1>
<Font Color = "aqua"> <b> This is Fred's Home Page </b> </Font>
</H1>
<p> <H2> Hi! My name is Fred, and this web site is all about me! </H2> </p>
</Center>
<HR Align = Center>
<br> <br> <br>
<p> <H> Here are some things I want to share: </H3> </p>
<br>
<br>My Hobbies
<br>My Pets
<p> <H2><Font Color = "White"><b>Here is a picture I like! </b> </Font></H2> </p>
<IMG src="bfly1.gif" align = left >
</Body>
</HTML>
------------------------------------------------------------------------------------------------------------------------------------------------------------
file2.txt
<HTML>
<Head>
<Title> Fred's Home Page </Title>
</Head>
<Body bgcolor = "white">
<Center>
<H1>
<Font Color = "aqua"> <b> Fred's Home Page </b> </Font>
</H1>
<p> <H2> Hi! My name is Fred, and this web site is all about me! </H2> </p>
</Center>
<HR Align = Center>
<br> <br> <br>
<p> <H> Here are some things I want to share: </H3> </p>
<br>
<br>My Hobbies
<br>My Pets
<p> <H2><Font Color = "White"><b>Here is a picture I like! </b> </Font></H2> </p>
<IMG src="bfly1.gif" align = left >
</Body>
</HTML>
------------------------ ------------------------ ------------------------ ------------------------
Sample output:
Line : 8
<Font Color = "aqua"> <b> This is Fred's Home Page </b> </Font>
<Font Color = "aqua"> <b> Fred's Home Page </b> </Font>
Note : The two text file file1.txt and file2.txt must be in the directory directory or folder when running the java file or if using eclipse, then add files to the project folder by dragging files onto the project name .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.