android快速遍历目录及查找文件
来源:互联网 发布:淘宝supreme正品店 编辑:程序博客网 时间:2024/04/30 10:50
java中遍历目录,可以使用递归的方法:
SearchFile(File[] files){for (File file : files){if (file.isDirectory())//若为目录则递归查找{SearchFile(file.listFiles());}else if (file.isFile()){String path = file.getPath();if (path.endsWith(".gbc"))//查找指定扩展名的文件{//do somethHashMap<String,Object> map;map = new HashMap<String,Object>();map.put("ItemImage", R.drawable.img);map.put("ItemTitle", path);listItem.add(map);}}}}
使用:
String path="/sdcard";File[] files = new File(path).listFiles();SearchFile(files);
在sdcard中有很多目录和文件时,上述方法非常的慢,可能要经历几分钟才能完成,基本上不能实用。想不出更好的算法,只能是怀疑java太慢了,
改用c来做应该会快很多,于是用NDK试了一下,果然快了很多:
#include<sys/types.h>#include<dirent.h>#include<unistd.h>void ListPath(char* path){DIR * dir;struct dirent * ptr;dir =opendir(path);char currfile[1024]={0}; int len = strlen(path);if(path[len-1] != '/'){path[len] = '/';path[len+1] = 0;}if( dir == NULL){ return; }while((ptr = readdir(dir))!=NULL){if(strcmp(ptr->d_name, ".")==0||strcmp(ptr->d_name,"..")==0)continue;sprintf(currfile,"%s%s",path,ptr->d_name); if (ptr->d_type==8)//普通文件{char *p=ptr->d_name + strlen(ptr->d_name)-4;if (strcmp(p,".gbc")==0){strcpy(romPaths[romCnt], currfile);//把文件路径保存起来romCnt++;}}else if (ptr->d_type==4)//目录{ListPath(currfile);}}closedir(dir);}
- android快速遍历目录及查找文件
- 遍历目录查找文件
- c++ 遍历目录查找文件
- CFileFind遍历目录查找文件
- python3遍历目录查找文件
- 遍历文件及目录
- php遍历目录及文件
- 遍历目录及替代文件
- 遍历整个目录树查找文件
- 遍历整个目录树查找文件
- 如何遍历整个目录树查找文件
- QT 遍历目录查找指定文件
- 多进程遍历目录并查找文件
- 递归法遍历目录查找文件
- python常用文件查找/目录遍历方法
- linux下查找目录及查找文件
- 遍历目录文件并查找文件中的内容
- Linux下遍历所有文件及目录
- Organization Defaults by Permission List
- 实现TXT导入ListView
- SSH2整合步骤
- 实现IP地址输入框
- 关于HTML5的预测
- android快速遍历目录及查找文件
- listview增加一个复选框
- 如何编写一个完整的Linux命令
- 设置 Ubuntu 联通宽带上网
- delphi中listview选择整行
- c/C++对mysql数据库进行操作笔记
- Android日记之2012/01/06
- 汤姆大叔的博客 javascript 深入理解JavaScript系列
- DLL动态库中调用MFC资源