Consider 1000 files in the folder.Now i want to find the same file in that folder.
I tired it by byte by byte comparison but it took long time to finish.
This is the code
fs1=new BufferedInputStream(new FileInputStream(file1));
fs2=new BufferedInputStream(new FileInputStream(file2));
int b1,b2;
do
{
b1=fs1.read();
b2=fs2.read();
if(b1!=b2)
{
match=false;
break;
}
}while(found && b1 !=- 1);
if(match)
{
Log.e("cyb", "Matched");
}
Any other method to find the same file?
First thing you should do to optimize your code is to check sizes of the files you compare. If the sizes are not the same then there is no point in reading the files into memory and comparing them byte by byte.
Another thing you can do is to compute a CRC for each of the file first, and then do the actual comparison only for files which have the same CRC (and the same length). This should greatly limit the number of your expensive byte-by-byte comparisons if you are dealing with many different files of the same length.