We help IT Professionals succeed at work.

Compare the line in a text file

motolanix1
motolanix1 asked
on
279 Views
Last Modified: 2013-11-23
Hi there,

Please see my code below. Im stuck in the sense that I want to compare two txt files and the code to read and compare each line in the file in both files. If the content in any of the lines does not match, then it should inform me at the end of running code which liines do not match.

Could one expert perhaps have a look at the code and tweak it so this it performs the above functionality.
Speedy response is very much appreciated.

Code
--------

/**
 * @(#)file_comparison.java
 *
 *
 * @author
 * @version 1.00 2007/12/13
 */

import java.io.*;

public class file_comparison {
      
            public static void main(String[] args) {
               
               try {
       
            BufferedReader in1 = new BufferedReader(new FileReader("C:\\SAMPLE1.txt"));
                BufferedReader in2 = new BufferedReader(new FileReader("C:\\SAMPLE2.txt"));
                
            String line1;
            String line2;
 
            StringBuilder sb1 = new StringBuilder();
            StringBuilder sb2 = new StringBuilder();
 
            while ((line1 = in1.readLine()) != null) {
                sb1.append(line1);
                System.out.println("Data in file 1 is:" + line1 +"\n");
                }
 
            while ((line2 = in2.readLine()) != null) {
                sb2.append(line2);
                System.out.println("Data in file 2 is:" + line2 +"\n");
            }
 
            String file1 = new String(sb1);
            String file2 = new String(sb2);
           
            if (file1.equals(file2)) {
                System.out.println("\nScanned each row in file...Data in file match");
            } else {
 
                System.out.println("\nScanned each row in file...Data in file do not match");
            }
        }
 
        catch (IOException e) {
            e.printStackTrace();
        }
      }
}
Comment
Watch Question

Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Hi there, thats great but for some reason when i run the code it doesnt give me any output when i run the code. It just says process completed. I have added the following libraries with the code:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.*;

Do you know why i am having this problem? Could you also perhaps comment the code? Thanks

Commented:
ok, I commented the code and added more printouts of info for it.

I checked and the code works
I have 2 files with 3 text lines in each
I do the compare and it shows the differences

please send here the files you are trying to compare if the code still doesnt work
(also make sure the files are located at C:\SAMPLE1.txt and C:\SAMPLE2.txt)
package ConsoleApplication1;
 
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.*;
 
/**
 * Summary description for Program
 */
public class Program
{
	public static void main(String[] args)
	{
		try
		{
 
			BufferedReader in1 = new BufferedReader(new FileReader("C:\\SAMPLE1.txt"));
			BufferedReader in2 = new BufferedReader(new FileReader("C:\\SAMPLE2.txt"));
 
			String line1;
			String line2;
			int line_number = 0;
			int total_lines = 0;
			int mismatched_lines = 0;
 
			// read a single line from the first file
			while ((line1 = in1.readLine()) != null)
			{
				// update our counters
				line_number++;	// current line number
				total_lines++;	// total lines so far (actually same but no matter)
 
				// read a single line from the second file
				line2 = in2.readLine();
 
				// make sure that this line is valid (not end of file)
				//  and if so check it the 2 lines are identical
				if ((line2 != null) && (!line1.equals(line2)))
				{
					// if here we found mismatched lines
					mismatched_lines++;
 
					// show which lines, you can change this notification
					System.out.println("** Found lines that don't match in linenumber "+line_number);
					// show the actual line contents (that dont match), remove if you dont want this
					System.out.println(line1);
					System.out.println(line2);
					System.out.println();
					// you can also use line_number to print which line number it is
				}
			}
 
			// print out summary statistics
			System.out.println("*** Checked " + total_lines + " lines and found " + mismatched_lines+" mismatches");
 
		}
 
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
}

Open in new window

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.