How do I count the number of files with a specific extension on Android?

16,003

Solution 1

Some tests showed me that the default directory where Android stores files created by an application using Context.getFilesDir() is /data/data/<your_package_name>/files

To count the files in any given directory you use File.listFiles(FileFilter) over the root dir. Your FileFilter should then be something like this (to filter for ".rbc" files):

public static class RBCFileFilter implements FileFilter {

    @Override
    public boolean accept(File pathname) {
        String suffix = ".rbc";
        if( pathname.getName().toLowerCase().endsWith(suffix) ) {
            return true;
        }
        return false;
    }

}

If you have some kind of directory structure you need to recursively search then you will have to File.listFiles(FileFilter) over the entire directory structure. And it should be something like:

public static List<File> listFiles(File rootDir, FileFilter filter, boolean recursive) {
    List<File> result = new ArrayList<File>();
    if( !rootDir.exists() || !rootDir.isDirectory() ) 
        return result;


    //Add all files that comply with the given filter
    File[] files = rootDir.listFiles(filter);
    for( File f : files) {
        if( !result.contains(f) )
            result.add(f);
    }

    //Recurse through all available dirs if we are scanning recursively
    if( recursive ) {
        File[] dirs = rootDir.listFiles(new DirFilter());
        for( File f : dirs ) {
            if( f.canRead() ) {
                result.addAll(listFiles(f, filter, recursive));
            }
        }
    }

    return result;
}

And where DirFilter would implements FileFilter this way:

public static class DirFilter implements FileFilter {

    @Override
    public boolean accept(File pathname) {
        if( pathname.isDirectory() ) 
            return true;

        return false;
    }

}

Solution 2

Android usually stores files created by an application in data/data/package_name_of_launching_Activity and there you'll find a few folders where files can be stored. You can get a cache directory within that path by calling getCacheDir().

A quick strategy for counting specific extensions could be as follows:

If you have a folder, say File folder = new File(folderPath) where folderPath is the absolute path to a folder. You could do the following:

String[] fileNames = folder.list();
int total = 0;
for (int i = 0; i< filenames.length; i++)
{
  if (filenames[i].contains(".rbc"))
    {
      total++;
     }
  }

This can give you a count of the total files with ".rbc" as the extension. Although this may not be the best/efficient way of doing it, it still works.

Share:
16,003
Rob S.
Author by

Rob S.

Currently working at Confluent

Updated on June 04, 2022

Comments

  • Rob S.
    Rob S. almost 2 years

    In the app I'm developing for Android I'm letting users create files specific to my application with the extension ".rbc". So far I have been successful in creating, writing to, and reading from these files.

    Right now I am trying to count the number of these files that exists. I'm not particularly familiar with Java and I'm just beginning programming for Android so I feel a bit lost. All of my attempts so far at doing this have not been able to locate any files with my extension.

    So I basically have two questions I need answered so that I can figure this out:

    Where is the default directory where Android stores files created by an application?

    Do you have any examples do you can give me of counting files with a specific extension on Android?

    Thank you very much in advance for your time.

  • Rob S.
    Rob S. about 13 years
    What class is listFiles() a method of?
  • Tiago
    Tiago about 13 years
    It must be pointed out that Context.getCacheDir() gives you the location of /data/data/<package-name>/cache and Context.getFilesDir() points to /data/data/<package-name>/files. The former is flushed regularly so be aware when storing files in the cache dir.