• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

compare two text files

 
cool dude
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am comparing two text file by checking occurance of a line in file 1 by comparing it with each line in file 2(not line by line)
i have to print in 3rd text file as difference
please see my progrm and tell me modification required


package comp.vnet.comparator;

import java.io.*;
import comp.vnet.comparator.NewFile;


class FileComparator {
public static void main(String[] args) throws IOException{

String file1,file2,String1,String2;
BufferedReader br1,br2;
int fileCount1=0;
int fileCount2=0;
br1= new BufferedReader(new InputStreamReader (System.in));


// File file = new File ("output.txt");
FileWriter fstream = new FileWriter("out.txt");


// FileOutputStream fo = new FileOutputStream("E:/Filecomparator/FileComparator/output.txt");
System.out.println("Enter First file name");
file1="b.txt";
//file1=br1.readLine();
System.out.println("Enter Second file name");
file2="a.txt";
//file2=br2.readLine();
NewFile nf= new NewFile();
br1=nf.creatingFile(file1);
br2=nf.creatingFile(file2);

while ((String1= br1.readLine()) != null) {
fileCount1++;
}
while ((String2= br2.readLine()) != null) {
fileCount2++;
}
System.out.println("fileCount1+ : " + fileCount1);
System.out.println("fileCount2+ : " + fileCount1);

br1=nf.creatingFile(file1);
BufferedWriter out = new BufferedWriter(fstream);

for(int i=0;i<=fileCount1;i++)
{
br2=nf.creatingFile(file2);
String1=br1.readLine();
for(int j=0;j<fileCount2;j++)
{
String2=br2.readLine();
if ( String1.equals (String2) ) {
System.out.println("the line is equal");
}
else{

out.write(String1);
System.out.println(String1);

}



}

}
out.close();
br1.close();
br2.close();
}


}
 
Joe Ess
Bartender
Posts: 9214
9
Linux Mac OS X Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the JavaRanch, Cool Dude.
First, you must have missed our naming policy on the way in. To keep things friendly around here we ask that people use their real name, that is, a first and last name, seperated by a space and not obviously fake. You can change your displayed name here. Thanks for your prompt attention.
Second, we get a lot of people coming here with schoolwork so we ask that you Do Your Own Homework. Have a look at our FAQ: How To Ask Questions On JavaRanch and feel free to come back with any specific questions you have.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can probably Google on "text diff java" and find a lot of tips on algorithms. There are some serious "computer science" solutions that are so compact I have no idea what they do, and some that make more sense. Let us know what you find!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic