现在的位置: 首页 > 综合 > 正文

Android 遍历文件夹,搜索指定扩展名的文件

2018年03月20日 ⁄ 综合 ⁄ 共 665字 ⁄ 字号 评论关闭
private
List<String> lstFile =
new
ArrayList<String>(); 
//结果 List
2   
3 public
void
GetFiles(String Path, String Extension,
boolean IsIterative) 
//搜索目录,扩展名,是否进入子文件夹
4 {
5     File[] files =
new File(Path).listFiles();
6   
7     for
(int
i =
0; i < files.length; i++)
8     {
9         File f = files[i];
10         if
(f.isFile())
11         {
12             if
(f.getPath().substring(f.getPath().length() - Extension.length()).equals(Extension)) 
//判断扩展名
13                 lstFile.add(f.getPath());
14   
15             if
(!IsIterative)
16                 break;
17         }
18         else
if (f.isDirectory() && f.getPath().indexOf("/.") == -1
//忽略点文件(隐藏文件/文件夹)
19             GetFiles(f.getPath(), Extension, IsIterative);
20     }
21 }

 

抱歉!评论已关闭.