Java Program: Compare two text files

By Angsuman Chakraborty, Gaea News Network
Wednesday, April 29, 2009

java_logoToday while managing the comments, I got a request of this sort.

HI,

I have a small question in java.
I have two inputs , which are in two documents .I want to compare these two document values. Is it possible in Java.  please let me know.

thanks & Regards,

Certainly, there are ways to compare two text files in Java but the return value can be of many types.

A Java file difference utility can

  • compare files character by character.
  • give the number of differences between 2 files.
  • ignore white space when comparing.

Our program will prompt for 2 file names (.txt) that are to be compared. Given the file names, this program will compare the two files. If any difference is there, it will print the differences and no. of differences in command prompt. If no difference is there, it will print files are equal.

import java.io.*;
import java.util.*;

public class myfilereader
{
public static void main (String[] args) throws java.io.IOException
{
//Getting the name of the files to be compared.
BufferedReader br2 = new BufferedReader (new
InputStreamReader(System.in));
System.out.println(”Enter 1st File name:”);
String str = br2.readLine();
System.out.println(”Enter 2nd File name:”);
String str1 = br2.readLine();

String s1=”";
String s2=”",s3=”",s4=”";
String y=”",z=”";

//Reading the contents of the files
BufferedReader br = new BufferedReader (new FileReader (str));
BufferedReader br1 = new BufferedReader (new FileReader (str1));

while((z=br1.readLine())!=null)
s3+=z;

while((y=br.readLine())!=null)
s1+=y;

System.out.println ();

//String tokenizing
int numTokens = 0;
StringTokenizer st = new StringTokenizer (s1);
String[] a = new String[10000];
for(int l=0;l<10000;l++)
{a[l]=”";}
int i=0;
while (st.hasMoreTokens())
{
s2 = st.nextToken();
a[i]=s2;
i++;
numTokens++;
}

int numTokens1 = 0;
StringTokenizer st1 = new StringTokenizer (s3);
String[] b = new String[10000];
for(int k=0;k<10000;k++)
{b[k]=”";}
int j=0;
while (st1.hasMoreTokens())
{
s4 = st1.nextToken();
b[j]=s4;
j++;
numTokens1++;
}

//comparing the contents of the files and printing the differences, if any.
int x=0;
for(int m=0;m<a.length;m++)
{
if(a[m].equals(b[m])){}
else
{
x++;
System.out.println(a[m] + ” — ” +b[m]);
System.out.println();}
}
System.out.println(”No. of differences : ” + x);
if(x>0){System.out.println(”Files are not equal”);}
else{System.out.println(”Files are equal. No difference found”);}

}
}

[code source: sourcecodesworld.com]

Between, if you have any query on programming, trouble shooting, computer maintenance etc, feel free to ask us. We will reply you as fasr as we can.

Discussion

kalyx
May 27, 2010: 6:15 am

doesn’t work.

can you send me the solution?


kalyx
May 27, 2010: 6:14 am

doesn’t work


Priyanka
May 4, 2010: 11:34 pm

Hi,
I want to compare multiple files at a time and the user should be prompted to enter the folder name where all the files to be compared are stored.
And also it should mark the differences if any.


Larity
April 21, 2010: 3:20 am

Hi, I want to Know the codings in java for Creating a Scholl Admission form


Vivek
December 15, 2009: 6:12 am

Is there any simple java code to compare two documents?? (without using tokens as I am new to tokens)

December 3, 2009: 2:10 pm

The proper format of the for loop is;

for(int m=0; m < a.length ;m++) {


faiza
December 3, 2009: 2:35 am

how can I compare 2 text files and output the same line in php?
I need to call that file from database first..
Files are store in 1 folder and save the link in database..

any1?


Sudharshan
October 12, 2009: 9:24 am

Comparing any two files with any extension in java - techelp.in/home/?p=19


sindhu
September 26, 2009: 7:01 pm

Hi,
Can some body help me in explaining how to compare a text file and a table in database(MySQL).


Amit
July 5, 2009: 6:36 pm

hi i want a solution of one loop question by buffer reader which is as follows

1*16+2*15+3*14…….16*1


Kapil
June 16, 2009: 7:35 am

Hi,

How to compare two strings in java using while loop for check that if string 1 is different from 2 then loop can work wheither the condition is satisfied.


Tulasi
May 28, 2009: 4:29 am

Hi,

How to compare few java files and produce the output to spread sheet.

Regards,
Tulasi


Deepa
May 28, 2009: 4:27 am

Hi,

How to compare few files and produce the output in spread sheet.

Thanks in advance!!!

Regards,
Tulasi Subramaniam


Deepak
May 17, 2009: 4:56 pm

Hello friends,
I want to ask that i have to compare two large database records (1st of 20000 records, 2nd of 15000 records) and after comparing, output is the common(same) record in both database files.i need the source code for that.
I have wrote a code but when i try to compare more then 20 records my PC hung and shut down automatically.
Plz help me


Allen
May 13, 2009: 1:50 am

I come across this code looking for a quick script, however, this code has a number of issues.
1) In the for loop, it was written:
for(int m=0;m
{
The for loop is not closed and m is not specified.
2) The file concatenates the text files into strings and compare them token by token (or at least that’s what i think it does, given the incomplete for loop). This doesnt taken into account of the actual line differences, which is important and implemented in such programs as the Unix diff.

YOUR VIEW POINT
NAME : (REQUIRED)
MAIL : (REQUIRED)
will not be displayed
WEBSITE : (OPTIONAL)
YOUR
COMMENT :