1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
package com.example; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.security.MessageDigest; import java.util.Arrays; import java.util.HashMap; import java.util.Iterator; import java.util.Set; public class Compare { //This can be any folder locations which you want to compare File dir1 = new File("C:\\ComparisonFolder\\master"); File dir2 = new File("C:\\ComparisonFolder\\release"); public static void main(String ...args) { Compare compare = new Compare(); try { compare.getDiff(compare.dir1,compare.dir2); } catch(IOException ie) { ie.printStackTrace(); } } public void getDiff(File dirA, File dirB) throws IOException { File[] fileList1 = dirA.listFiles(); File[] fileList2 = dirB.listFiles(); Arrays.sort(fileList1); Arrays.sort(fileList2); HashMap<String, File> map1; if(fileList1.length < fileList2.length) { map1 = new HashMap<String, File>(); for(int i=0;i<fileList1.length;i++) { map1.put(fileList1[i].getName(),fileList1[i]); } compareNow(fileList2, map1); } else { map1 = new HashMap<String, File>(); for(int i=0;i<fileList2.length;i++) { map1.put(fileList2[i].getName(),fileList2[i]); } compareNow(fileList1, map1); } } public void compareNow(File[] fileArr, HashMap<String, File> map) throws IOException { for(int i=0;i<fileArr.length;i++) { String fName = fileArr[i].getName(); File fComp = map.get(fName); map.remove(fName); if(fComp!=null) { if(fComp.isDirectory()) { getDiff(fileArr[i], fComp); } else { String cSum1 = checksum(fileArr[i]); String cSum2 = checksum(fComp); if(!cSum1.equals(cSum2)) { System.out.println(fileArr[i].getName()+"\t\t"+ "different"); } else { System.out.println(fileArr[i].getName()+"\t\t"+"identical"); } } } else { if(fileArr[i].isDirectory()) { traverseDirectory(fileArr[i]); } else { System.out.println(fileArr[i].getName()+"\t\t"+"only in "+fileArr[i].getParent()); } } } Set<String> set = map.keySet(); Iterator<String> 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()); } } } public void traverseDirectory(File dir) { File[] list = dir.listFiles(); for(int k=0;k<list.length;k++) { if(list[k].isDirectory()) { traverseDirectory(list[k]); } else { System.out.println(list[k].getName() +"\t\t"+"only in "+ list[k].getParent()); } } } public String checksum(File file) { try { InputStream fin = new FileInputStream(file); java.security.MessageDigest md5er = MessageDigest.getInstance("MD5"); byte[] buffer = new byte[1024]; int read; do { read = fin.read(buffer); if (read > 0) md5er.update(buffer, 0, read); } while (read != -1); fin.close(); byte[] digest = md5er.digest(); if (digest == null) return null; String strDigest = "0x"; for (int i = 0; i < digest.length; i++) { strDigest += Integer.toString((digest[i] & 0xff) + 0x100, 16).substring(1).toUpperCase(); } return strDigest; } catch (Exception e) { return null; } } } |
Thanks for the program. helped me a lot.. ๐
Glad we could help ๐
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
Did you use the above code snippets with thread…if yes, then It won’t work.
follow the link to get more info.
http://docs.oracle.com/javase/7/docs/api/java/util/ConcurrentModificationException.html
What is the use of Checksum() function ?
please xpln checksum function
Thank you so much. It was very handy.
I was looking for a great code like this, Thank You.
How can we write all the different items to a txt file?
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) ๐
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?
To compare the contents of two files is a different use-case altogether and can’t be achieved via the current code as such. We do not have a readymade code for achieving the same as of now but would try and add a sample code on our site. Till then, you could refer to this link: https://www.ablebits.com/office-addins-blog/2016/02/25/compare-two-excel-files-sheets/
Cheers
๐
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.
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
Hello Anupam,
Thanks for the program
It is very useful for me.
Thanks,
Monika
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);
What is the use of the Checksum() function?
Google it up – checksum is a very common term used in software programming.
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.
Thank you Ram!
Let us know what difficulties you’re facing?
Hi Anupam, great code, I need one more help
I want to compare only a certain files like *.sql
Where should I check this?
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