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

In the application that I am developing for Android, I allow users to create files specific to my application with the extension ".rbc". So far I have managed to create, write and read from these files.

Now I am trying to count the number of these files that exist. I'm not particularly familiar with Java, and I'm just starting to program for Android, so I feel a little lost. All my attempts so far have not been able to find files with my extension.

Therefore, I basically have two questions that I need to answer so that I can understand this:

Where is the default directory, where does Android store files created by the application?

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

Thank you in advance for your time.

+3
source share
3 answers

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

To count the files in any directory you use File.listFiles(FileFilter)for the root directory. Your FileFilter should be something like this (to filter ".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 search recursively, then you will have File.listFiles(FileFilter)to go through 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 it DirFilterwill be implemented FileFilteras follows:

public static class DirFilter implements FileFilter {

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

        return false;
    }

}
+8

Android , data/data/package_name_of_launching_Activity, , . , getCacheDir().

:

, File folder = new File(folderPath), folderPath - . :

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

".rbc". , , , .

+2

:

# public static List getFiles(File aStartingDir)   
# {  
#     List result = new ArrayList();  
#    
#     File[] filesAndDirs = aStartingDir.listFiles();  
#     List filesDirs = Arrays.asList(filesAndDirs);  
#     Iterator filesIter = filesDirs.iterator();  
#     File file = null;  
#     while ( filesIter.hasNext() ) {  
#       file = (File)filesIter.next();  
#       result.add(file); //always add, even if directory  
#       if (!file.isFile()) {  
#         //must be a directory  
#         //recursive call!  
#         List deeperList = getFileListing(file);  
#         result.addAll(deeperList);  
#       }  
#    
#     }  
#     Collections.sort(result);  
#     return result;  
#   }  

fileFilter listFiles() .rbc.

EDIT: , . , /. Eclipse DDMS.

0
source

Source: https://habr.com/ru/post/1793417/


All Articles