Results 1 to 5 of 5
  1. #1
    trkece is offline Member
    Join Date
    Feb 2011
    Posts
    22
    Rep Power
    0

    Default 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

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,782
    Rep Power
    7

    Default

    Quote Originally Posted by trkece View Post
    Hi all,

    I want code/soltn
    No problems. You relax and put your feet up while I write the code for you.

  3. #3
    trkece is offline Member
    Join Date
    Feb 2011
    Posts
    22
    Rep Power
    0

    Default

    if u can provide code that wil be fine dats y i have mentioned solution also

  4. #4
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    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.

  5. #5
    trkece is offline Member
    Join Date
    Feb 2011
    Posts
    22
    Rep Power
    0

    Default

    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

  1. Comparing two XML files
    By anuj_sharma in forum XML
    Replies: 1
    Last Post: 02-07-2011, 09:09 AM
  2. Comparing Two Text Files
    By coder09 in forum New To Java
    Replies: 15
    Last Post: 03-03-2009, 06:11 AM
  3. [SOLVED] Comparing two files
    By Doctor Cactus in forum New To Java
    Replies: 7
    Last Post: 02-27-2009, 10:42 PM
  4. How to Extract EAR files in UNIX
    By RG18173 in forum Advanced Java
    Replies: 6
    Last Post: 12-12-2008, 08:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •