我如何计算文件的数量与Android的特定扩展名? [英] How do I count the number of files with a specific extension on Android?

查看:122
本文介绍了我如何计算文件的数量与Android的特定扩展名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在应用程序,我开发的机器人我让用户创建特定的文件到我的扩展名为.rbc应用程序。到目前为止,我已经创建,写入,并从这些文件看是成功的。

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.

现在我想指望这些文件存在的数量。我不是特别熟悉Java,我刚刚开始编程的Andr​​oid让我感到有点失落。我所有的努力,到目前为止,在做这个一直没能找到任何文件,我的分机。

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:

哪里是哪里的Andr​​oid的文件存储应用程序创建的默认目录?

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

你有什么例子你能给与Android的特定扩展名的文件计数的我?

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.

推荐答案

一些测试表明我在那里的Andr​​oid存储文件由应用程序使用创建的默认目录 Context.getFilesDir() /数据/数据​​/< your_package_name> /文件

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

要算在你使用 File.listFiles(的FileFilter)上的根目录,任何给定目录中的文件。那么你的FileFilter应该是这样的(为.rbc文件过滤器):

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;
    }

}

如果你有某种目录结构需要递归搜索,那么你将不得不 File.listFiles(的FileFilter)在整个目录结构。它应该是这样的:

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;
}

和其中 DirFilter 将工具的FileFilter 是这样的:

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;
    }

}

这篇关于我如何计算文件的数量与Android的特定扩展名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆