Results 1 to 5 of 5
- 02-16-2011, 05:46 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 22
- Rep Power
- 0
Comparing two files and extract the difference
Hi all,
I want code/soltn for comparing two files and extract the difference write it to the new file
Line1 in file1 compares with all the lines in file2 and similarly for all the lines ....
it's not line by line, each line in file1 compares it to the all the lines in file2
Thanx
RK
- 02-16-2011, 05:52 AM #2
- 02-16-2011, 06:04 AM #3
Member
- Join Date
- Feb 2011
- Posts
- 22
- Rep Power
- 0
if u can provide code that wil be fine dats y i have mentioned solution also
- 02-16-2011, 06:08 AM #4
I'm sure someone here will be happy to provide all the code for you. No one on this forum has that kind of time to put toward your project.
If you want to actually learn to write the code, then start with some tutorials and come here with specific questions.
- 02-16-2011, 06:11 AM #5
Member
- Join Date
- Feb 2011
- Posts
- 22
- Rep Power
- 0
import java.io.*;
public class FileExample {
public static void main(String[] args) throws IOException {
String actualFile = "write.txt";
String expectedFile = "kk.txt";
String descptnFile = "Write2.txt";
try{
// Create FileReader & Writer Objects.
FileReader actualFileReader = new FileReader(actualFile);
FileReader expctdFileReader = new FileReader(expectedFile);
FileWriter resultDesc = new FileWriter(descptnFile);
// Create Buffered Object.
BufferedReader actlFileBufRdr = new BufferedReader(actualFileReader);
BufferedReader expcFileBufRdr = new BufferedReader(expctdFileReader);
BufferedWriter resultFileBufWrtr = new BufferedWriter(resultDesc);
// Get the file contents into String Variables.
String actlFileContent = actlFileBufRdr.readLine();
String expctdFileContent = expcFileBufRdr.readLine();
// Compare the Contents of the files.
String startOfComparision = "---------START----------";
//resultFileBufWrtr.write(startOfComparision);
// resultFileBufWrtr.newLine();
System.out.println(startOfComparision);
boolean isDifferent = false;
int lineNumber = 1;
if (actlFileContent != null || expctdFileContent != null) {
// Check whether Actual file contains data or not
while((actlFileContent!=null) ){
// Check whether Expected file contains data or not
if (((expctdFileContent )!=null)) {
// Check whether both the files have same data in the lines
if (!actlFileContent.equals(expctdFileContent)) {
resultFileBufWrtr.write(""+actlFileContent+":"+exp ctdFileContent);
resultFileBufWrtr.newLine();
System.out.println("Difference in Line "+lineNumber+" :- Actual File contains :"+actlFileContent+", Expected File Contains : "+expctdFileContent);
isDifferent = true;
}
lineNumber = lineNumber+1;
expctdFileContent= expcFileBufRdr.readLine();
}
else{
resultFileBufWrtr.write(""+actlFileContent+":"+exp ctdFileContent);
resultFileBufWrtr.newLine();
System.out.println("Difference in Line "+lineNumber+" :- Actual File contains :"+actlFileContent+", Expected File Contains - "+expctdFileContent);
isDifferent = true;
lineNumber = lineNumber+1;
}
actlFileContent=actlFileBufRdr.readLine();
}
// Check for the condition : if Actual File has Data & Expected File doesn't contain data.
while ((expctdFileContent!=null)&&(actlFileContent==null )) {
resultFileBufWrtr.write(""+actlFileContent+":"+exp ctdFileContent);
resultFileBufWrtr.newLine();
System.out.println("Difference in Line "+lineNumber+" :- Actual File contains :"+actlFileContent+", Expected File Contains - "+expctdFileContent);
isDifferent = true;
lineNumber = lineNumber+1;
expctdFileContent= expcFileBufRdr.readLine();
}
}
else{
// Mention that both the files don't have Data.
resultFileBufWrtr.write(""+actlFileContent+":"+exp ctdFileContent);
resultFileBufWrtr.newLine();
System.out.println("Difference in Line "+lineNumber+" :- Actual File contains :"+actlFileContent+", Expected File Contains - "+expctdFileContent);
isDifferent = true;
}
// Check is there any difference or not.
String endOfComparision = "-----------END----------";
if (isDifferent) {
//resultFileBufWrtr.append(endOfComparision);
System.out.println(endOfComparision);
}
else{
resultFileBufWrtr.append("No difference Found");
resultFileBufWrtr.newLine();
resultFileBufWrtr.append(endOfComparision);
System.out.println("No difference Found");
System.out.println(endOfComparision);
}
actualFileReader.close();
expctdFileReader.close();
resultFileBufWrtr.close();
actlFileBufRdr.close();
expcFileBufRdr.close();
}
catch( FileNotFoundException e )
{
e.printStackTrace();
}
}
}
I wrote this code and pls tell me the modifications in this......
It's not printing difference b/w two files
Similar Threads
-
Comparing two XML files
By anuj_sharma in forum XMLReplies: 1Last Post: 02-07-2011, 09:09 AM -
Comparing Two Text Files
By coder09 in forum New To JavaReplies: 15Last Post: 03-03-2009, 06:11 AM -
[SOLVED] Comparing two files
By Doctor Cactus in forum New To JavaReplies: 7Last Post: 02-27-2009, 10:42 PM -
How to Extract EAR files in UNIX
By RG18173 in forum Advanced JavaReplies: 6Last Post: 12-12-2008, 08:21 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks