dythe
dythe

Reputation: 841

Iterate through a specified directory in Android

i am trying to develop a security application on Android and i want to iterate through the filenames of a specific directory so that i can compare the hash value of each file in the directory.

I have already found out how to do the hashing but for the iterating part i am confused on how it works.

Upvotes: 12

Views: 15016

Answers (1)

neevek
neevek

Reputation: 12148

You mean you want to traverse a directory recursively?

Something like this:

    public void traverse (File dir) {
       if (dir.exists()) {
        File[] files = dir.listFiles();
        if (files != null) {

        for (int i = 0; i < files.length; ++i) {
            File file = files[i];
            if (file.isDirectory()) {
                traverse(file);
            } else {
                // do something here with the file
            }
        }
      }
     }
    } 

Upvotes: 40

Related Questions