如何在遍历目录时指定目录的最大层数
2014-11-12 20:00
531 查看
有时候在查找文件的时候为了提高效率或者出于其它目的要对遍历的目录的层数作下限制,比如在查找自己创建或者下载的文件时,这些文件所在的目录一般都不会太深,
所以在文件比较多的时候,通过限制目录的层次可以极大的提高查找文件的效率,以下为代码:
所以在文件比较多的时候,通过限制目录的层次可以极大的提高查找文件的效率,以下为代码:
public static void findFile(String rootDir, String fileName, List<String> fileList,int depth,int maxDepth) { depth += 1; String tempName = null; File baseDir = new File(rootDir); if (!baseDir.exists() || !baseDir.isDirectory()){ return; } String[] list = baseDir.list(); if(list == null){ return; } for (int i = 0; i < list.length; i++) { File file = new File(rootDir + "/" + list[i]); if(!file.isDirectory()) { tempName = file.getName(); if(tempName.equals(fileName)){ fileList.add(file.getAbsolutePath()); return; } } else if(file.isDirectory()){ if(depth < maxDepth){ findFiles(rootDir + "/" + list[i],fileName,fileList,depth,maxDepth); } } } }
相关文章推荐
- Linux下如何遍历指定目录下的所有文件并删除指定天数之前创建的文件
- Java中如何利用File类递归的遍历指定目录中的所有文件和文件夹
- 递归遍历指定目录,获取该目录下最大的文件信息
- 如何利用Java遍历所有的指定文件目录下的所有文件
- vc中如何遍历指定的目录以及目录中的子目录
- 在Asp.net中如何遍历一个本地目录下的所有文件并上载到Web 服务器上指定的目录中
- 如何使用批处理,遍历指定目录下的文件。
- Ruby和SHELL中如何遍历指定目录的文件 推荐
- Linux下如何遍历指定目录下的所有文件并删除指定天数之前创建的文件
- JS如何遍历一个文件夹下的所有文件与目录
- 如何给VSFTP增加用户,只能访问指定目录
- [导入]如何在ASP.NET下遍历指定页面上所有控件
- 一个遍历指定目录下所有文件的例子
- 如何用idFTP遍历整个目录----下载、删除
- TIP:如何从VSS上获取虚拟目录并设定到指定位置
- 如何用idFTP遍历整个目录—下载、删除
- 如何在ASP.NET下遍历指定页面上所有控件
- 在C++Builder中实现遍历并载入指定目录
- 如何在ASP.NET下遍历指定页面上所有控件