如何计算 Android 上具有特定扩展名的文件数量?

发布于 2024-10-18 10:13:39 字数 324 浏览 1 评论 0原文

在我为 Android 开发的应用程序中,我允许用户创建特定于我的应用程序且扩展名为“.rbc”的文件。到目前为止,我已经成功地创建、写入和读取这些文件。

现在我正在尝试计算这些文件存在的数量。我对 Java 不是特别熟悉,而且我刚刚开始为 Android 编程,所以我感觉有点失落。到目前为止,我所做的所有尝试都无法找到带有我的扩展名的任何文件。

所以我基本上有两个问题需要回答,以便我能够弄清楚:

Android 存储应用程序创建的文件的默认目录在哪里?

你有什么例子可以给我在Android上计算具有特定扩展名的文件吗?

预先非常感谢您抽出时间。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

强者自强 2024-10-25 10:13:40

要对文件进行计数,您可以尝试以下代码:

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

您可以使用 listFiles() 方法的 fileFilter 参数来返回具有 .rbc 扩展名的文件。

编辑:抱歉,错过了一个问题。文件将放置在您的应用程序包中,该包位于
data/data.. 您可以使用 DDMS 模式在 Eclipse 的文件资源管理器中查看它们。

For counting files you can try this code:

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

You can use fileFilter parameter for listFiles() method to return files with .rbc extension.

EDIT: Sorry, missed one question. Files will be placed in your application package which will be there in
data/data.. You can view them in File Explorer in Eclipse with DDMS mode.

尤怨 2024-10-25 10:13:39

一些测试表明,Android 存储应用程序使用 Context.getFilesDir() 创建的文件的默认目录是 /data/data//files

来计数您在根目录上使用 File.listFiles(FileFilter) 的任何给定目录中的文件。你的 FileFilter 应该是这样的(过滤“.rbc”文件):

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)覆盖整个目录结构。它应该是这样的:

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

public static class DirFilter implements FileFilter {

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

        return false;
    }

}

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

}
旧瑾黎汐 2024-10-25 10:13:39

Android 通常将应用程序创建的文件存储在 data/data/package_name_of_launching_Activity 中,您会在其中找到一些可以存储文件的文件夹。您可以通过调用 getCacheDir() 获取该路径中的缓存目录。

计算特定扩展名的快速策略如下:

如果您有一个文件夹,请说Filefolder = 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”为扩展名的文件总数。尽管这可能不是最好/有效的方法,但它仍然有效。

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.

尐偏执 2024-10-25 10:13:39

这是一个 java 8+ 示例:

var total=Arrays.asList(new File(pathToDirectory).list())
                      .stream()
                      .filter(x -> x.contains(".rbc"))
                      .collect(Collectors.counting());

或者

var fileNames = Arrays.asList(tempDir.list())
                       .stream()
                       .filter(x -> x.contains(".rbc"))
                       .collect(Collectors.toList());
var total=fileNames.size();

Here is a java 8+ example:

var total=Arrays.asList(new File(pathToDirectory).list())
                      .stream()
                      .filter(x -> x.contains(".rbc"))
                      .collect(Collectors.counting());

or

var fileNames = Arrays.asList(tempDir.list())
                       .stream()
                       .filter(x -> x.contains(".rbc"))
                       .collect(Collectors.toList());
var total=fileNames.size();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文