To compare two folders (recursively) in Java 21

4.9/5 - (62 votes)

21 thoughts on “To compare two folders (recursively) in Java

  1. Reply Joe Nov 29,2014 2:26 pm

    Thanks for the program. helped me a lot.. ๐Ÿ™‚

  2. Reply Subha Jan 17,2015 11:01 am

    Hi Anupam,

    Can you please tell me what is the use of below code snippet :
    Set set = map.keySet();
    Iterator it = set.iterator();
    while(it.hasNext())
    {
    String n = it.next();
    File fileFrmMap = map.get(n);
    map.remove(n);
    if(fileFrmMap.isDirectory())
    {
    traverseDirectory(fileFrmMap);
    }
    else
    {
    System.out.println(fileFrmMap.getName() +”\t\t”+”only in “+ fileFrmMap.getParent());
    }

    Because while this snippet is present in the code it is failing with the below error :

    Exception in thread “main” java.util.ConcurrentModificationException
    at java.util.HashMap$HashIterator.nextEntry(HashMap.java:793)
    at java.util.HashMap$KeyIterator.next(HashMap.java:828)
    at com.compare.Compare.compareNow(Compare.java:104)
    at com.compare.Compare.getDiff(Compare.java:56)
    at com.compare.Compare.compareNow(Compare.java:71)

    But when I am commenting it. It works fine. Can you please help?

    Best Regards

  3. Reply Bobby Jun 23,2015 5:58 pm

    What is the use of Checksum() function ?

  4. Reply Bobby Jun 23,2015 5:59 pm

    please xpln checksum function

  5. Reply Siva Aug 14,2015 5:30 pm

    Thank you so much. It was very handy.

  6. Reply Feb May 17,2018 1:52 am

    I was looking for a great code like this, Thank You.
    How can we write all the different items to a txt file?

    • Reply Anupam Jain May 24,2018 6:55 pm

      Thanks for the appreciation.

      To write the differences you would need to put the file writing code in the place where System.out.println is used to print the difference on console. (We’re assuming you know file I/O in Java) ๐Ÿ™‚

  7. Reply Feb May 18,2018 2:34 am

    I somehow tried to write the result into a text file.
    How do we compare the contents of two excel file here using this script?
    This script output result based on date modified, how to compare the contents instead?

  8. Reply Sai kumar Sep 28,2018 1:18 pm

    anyone of you can help me in sending file paths dynamic, using post method in spring-boot and response should return the data which are identical and different.

    • Reply Anupam Jain Oct 3,2018 7:11 pm

      Hi Sai,

      Could you please elaborate a little more on the problem statement you’ve posted? An example would be great to help understand the same.

      -Thanks

  9. Reply Monika Mar 14,2019 8:13 am

    Hello Anupam,
    Thanks for the program
    It is very useful for me.
    Thanks,
    Monika

  10. Reply SJ May 9,2019 4:39 pm

    In case, anyone facing java.util.ConcurrentModificationException while iterating using Iterator at line 102, then instead of removing from Map remove from iterator itself. Like, for example,

    Instead of :
    map.remove(n);
    Do this :
    itr.remove(n);

  11. Reply Ayushi Jain Aug 28,2020 5:18 pm

    What is the use of the Checksum() function?

  12. Reply Anshuman Oct 10,2020 4:28 am

    Google it up – checksum is a very common term used in software programming.

  13. Reply Ram Jan 11,2021 11:02 am

    Hi Team,A great piece of code. I need one more help. I want to write a report in excel of present file or its directory.

  14. Reply Chandra Feb 24,2021 3:06 pm

    Hi Anupam, great code, I need one more help
    I want to compare only a certain files like *.sql
    Where should I check this?

    • Reply Anupam Jain Jun 27,2021 1:21 pm

      the else block in compareNow function where we calculate the checksum is an easy candidate for this. Although for better efficiency, you might want to make bigger changes and include only those files in map which are required. So instead of just dumping whatever is coming in fileList1 and fileList2 arrays, you could attempt to filter the files you desire

Leave a Reply