Method to check files+folders tree if they exist
my Android application downloads ~50 mb of data - 2500 files. I want to check if a file is missing. An idea is to make a List with all the paths of the files, and then check each file if(File.exists())..
But that will resu开发者_StackOverflow社区lt in 2500 times to check if it exists...
Is there a better way that this? Thanks.You can list all the files using
File[] files = mFolder.listFiles();
and
int totalFiles = files.length;
if the size doesn't match what you have been expecting then you would have to iterate through all of them, otherwise continue with your application.
Even if you wanted to take the performance hit of checking that all your expected files exist, there is no guarantee that their contents are in good shape. For example, the files could all be empty.
A better solution would be to compress the files into an archive on he server, then calculate a hash digest for the archive file. See MessageDigest JavaDoc for information on how to do this in Java.
After downloading the archive file, you can then re-calculate the digest on the client and compare with the server's version to confirm that the archive was downloaded correctly and remains uncorrupted.
精彩评论