import java.io.*;
public class Test {
public static void main(String[] args) throws Throwable {
System.out.print(duplicate(new File("C:\\Users\\Me\\Desktop\\4-01 Living the Dream.avi"),
new File("C:\\Users\\Me\\Desktop\\4-01 Living the Dream - Copy.avi")));
}
public static boolean duplicate(File f1, File f2) throws Throwable {
FileInputStream fis1 = new FileInputStream(f1),
fis2 = new FileInputStream(f2);
byte[] ba1 = new byte[fis1.available()],
ba2 = new byte[fis2.available()];
if (ba1.length == ba2.length) {
fis1.read(ba1);
fis2.read(ba2);
for (int i = 0; i < ba1.length; i++) {
if (ba1[i] != ba2[i]) return false;
}
return true;
}
return false;
}
}
Is this seriously the quickest way? I notice some redundancy when you read the FileInputStream into the byte array, and then check each byte. Is there a way to read the file byte-by-byte and check it on-the-fly? It would replace the byte array, to just one byte variable.
Last edited by Nim; December 31st, 2009 at 08:45 AM.
I added the paragraph below my code the minute you posted, in case you didn't catch that. I'm willing to bet that what I've described is in the API. I tried using FileInputStream.read(), but it takes way too long.
Last edited by Nim; December 31st, 2009 at 09:15 AM.
Well, if you are worried about reading in large files, you should probably use the buffered input stream read in (and compare) a line at a time. This way you won't run into the problem of trying to read EVERYTHING into memory all at once.
On my cra**y PC in the office (1 CPU core) took me about 2 and half min to calculate hash of a 4.4Gb linux iso image.
Last edited by Xeel; January 4th, 2010 at 05:30 PM.
Wanna install linux on a vacuum cleaner. Could anyone tell me which distro sucks better?
I had a nightmare last night. I was dreaming that I’m 64-bit and my blanket is 32-bit and I couldn’t cover myself with it, so I’ve spent the whole night freezing. And in the morning I find that my blanket just had fallen off the bed. =S(from: bash.org.ru)
//always looking for job opportunities in AU/NZ/US/CA/Europe :P
willCodeForFood(Arrays.asList("Java","PHP","C++","bash","Assembler","XML","XHTML","CSS","JS","PL/SQL"); USE [code] TAGS!Read this FAQ if you are new here. If this post was helpful, please rate it!
Bookmarks