File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes I/O and Streams and the fly likes compare two text files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "compare two text files" Watch "compare two text files" New topic
Author

compare two text files

cool dude
Greenhorn

Joined: Jun 19, 2007
Posts: 1
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

Joined: Oct 29, 2001
Posts: 8964
    
    9

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.


[How To Ask Questions On JavaRanch]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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!


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: compare two text files