how to compare contents of two files in java?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • afroz
    New Member
    • Sep 2010
    • 1

    how to compare contents of two files in java?

    i want a code in java in order to compare contents of two files in java
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    Hi afroz!

    We won't write that code for you, but we can help you write it yourself. But first of all, we need to know what the output should be. I can think of two possibilities:
    1. The output is either "true" or "false", depending on if they are the same or not.
    2. The differences are listed, maybe with some detail. A bit like diff maybe.
    No doubt, the second one is much more difficult than the first.

    Some general advice: We have an article on reading (and writing to) files here, you should read that.

    Greetings,
    Nepomuk

    Comment

    • xploreraj
      New Member
      • Jan 2010
      • 49

      #3
      Hi,
      I'm a java learner. Would you please clarify what are those differences and what are your expectations. And what type of logic would you use? I'm not understanding the case as of now.

      Regards :-)

      Comment

      • Nepomuk
        Recognized Expert Specialist
        • Aug 2007
        • 3111

        #4
        Hi!

        Although you're not the original poster, I'd be happy to explain. ^^

        So, first of all, let's assume you have two files you want to compare like these ones:
        • file1.txt:
          Code:
          This is
          my first
          file
        • file2.txt:
          Code:
          This is
          my second
          file
        Now, the first attempt would just compare "are the files identical?" which will of course give you "no" (or "false") as an answer. The way to do this would be to read the two files at the same time and compare if you're reading the same in both files.

        The second attempt would give you an output something like
        Code:
        < my first
        > my second
        Which means "compared to the first file, the second file had the line 'my first' removed and had the line 'my second' inserted." This kind of comparison is much more complex, as if you were to change file2.txt to
        Code:
        This is
        my second
        absolutely brilliant
        file
        the output would be something like
        Code:
        < my first
        > my second
        > absolutely brilliant
        so it can even recognise that line 3 in file1.txt and line 4 in file2.txt are the same. Now to do something like that, you'll probably have to read in one of the files line by line, save them to some kind of container and then read the other file line by line and check, if the line you just read was already in the first file. So, much more complicated.

        Greetings,
        Nepomuk

        Comment

        • xploreraj
          New Member
          • Jan 2010
          • 49

          #5
          Oh, thanks a lot...
          Would you please take some time to solve my tomcat problem threaded in the same forum as well...!

          Regards
          xploreraj :D

          Comment

          Working...