Results 1 to 8 of 8
  1. #1
    Doctor Cactus is offline Member
    Join Date
    Oct 2008
    Posts
    63
    Rep Power
    0

    Default [SOLVED] Comparing two files

    Hi, I have two .java files and I want to check if the code in both files is the same or not. I know about the FileReader class, but how do you compare them?

    Help is greatly appreciated.

  2. #2
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    282
    Rep Power
    6

    Default

    If you wish to check whether the files are identically same or not, probably the following might be the logic

    we start a loop
    String s=line from file1
    String s1=line from file2

    if(s.equals(s1))
    we found a match
    we continue the loop
    else
    no need to check further we break the loop

  3. #3
    Doctor Cactus is offline Member
    Join Date
    Oct 2008
    Posts
    63
    Rep Power
    0

    Default

    Thanks for the reply. I understand what you mean, but what if I have more than one statement on a line in file 1, and one statement per line in file2
    For example, in File1
    System.out.println(''Hello World''); System.out.println(''Hello World again'');

    in File2

    System.out.println(''Hello World'');
    System.out.println(''Hello World again'');

    Clearly, the code in both files is the same, but if I use your method of comparing line by line, I presume it won't work?

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    huh, no the code is not the same, because the output is different. I understand your problem. hhhmm... maybe when the case you mention is found, a popup shows the user the difference and asks if the comparation should continue. Or write the found differences to a file for later review.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  5. #5
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    maybe replace all line breaks with some kind of special character that you only check for when necessary?

  6. #6
    JVposter is offline Member
    Join Date
    Feb 2009
    Location
    Romania
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by Doctor Cactus View Post
    Hi, I have two .java files and I want to check if the code in both files is the same or not. I know about the FileReader class, but how do you compare them?

    Help is greatly appreciated.
    hello!

    File reader class it is for reading not for comparing. java have not support for file compare. For that you need to made your own program!
    You must read the content from to files and compare it line by line

    regards,

  7. #7
    Doctor Cactus is offline Member
    Join Date
    Oct 2008
    Posts
    63
    Rep Power
    0

    Default

    OK, thanks for all the replies. I will use the line by line way of comparison.

  8. #8
    chrono25 is offline Member
    Join Date
    Feb 2009
    Posts
    10
    Rep Power
    0

    Default

    i use a software called beyond compare and it works wonders!

Similar Threads

  1. Comparing two images
    By GhosT in forum Advanced Java
    Replies: 13
    Last Post: 04-25-2009, 12:37 AM
  2. Comparing Two Text Files
    By coder09 in forum New To Java
    Replies: 15
    Last Post: 03-03-2009, 06:11 AM
  3. comparing
    By Feng in forum New To Java
    Replies: 2
    Last Post: 11-23-2007, 09:40 AM
  4. Comparing JavaWebFrameworks
    By pegitha in forum Web Frameworks
    Replies: 1
    Last Post: 05-18-2007, 06:23 PM

Posting Permissions

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