Java递归搜索指定文件夹下的匹配文件
2014-07-06 14:13
429 查看
原文链接:
http://tiwson.iteye.com/blog/681888
http://tiwson.iteye.com/blog/681888
import java.io.File; import java.util.ArrayList; import java.util.List; import java.util.Queue; /** * @author tiwson 2010-06-02 * */ public class FileSearcher { /** * 递归查找文件 * @param baseDirName 查找的文件夹路径 * @param targetFileName 需要查找的文件名 * @param fileList 查找到的文件集合 */ public static void findFiles(String baseDirName, String targetFileName, List fileList) { /** * 算法简述: * 从某个给定的需查找的文件夹出发,搜索该文件夹的所有子文件夹及文件, * 若为文件,则进行匹配,匹配成功则加入结果集,若为子文件夹,则进队列。 * 队列不空,重复上述操作,队列为空,程序结束,返回结果。 */ String tempName = null; //判断目录是否存在 File baseDir = new File(baseDirName); if (!baseDir.exists() || !baseDir.isDirectory()){ System.out.println("文件查找失败:" + baseDirName + "不是一个目录!"); } else { String[] filelist = baseDir.list(); for (int i = 0; i < filelist.length; i++) { File readfile = new File(baseDirName + "\\" + filelist[i]); //System.out.println(readfile.getName()); if(!readfile.isDirectory()) { tempName = readfile.getName(); if (FileSearcher.wildcardMatch(targetFileName, tempName)) { //匹配成功,将文件名添加到结果集 fileList.add(readfile.getAbsoluteFile()); } } else if(readfile.isDirectory()){ findFiles(baseDirName + "\\" + filelist[i],targetFileName,fileList); } } } } /** * 通配符匹配 * @param pattern 通配符模式 * @param str 待匹配的字符串 * @return 匹配成功则返回true,否则返回false */ private static boolean wildcardMatch(String pattern, String str) { int patternLength = pattern.length(); int strLength = str.length(); int strIndex = 0; char ch; for (int patternIndex = 0; patternIndex < patternLength; patternIndex++) { ch = pattern.charAt(patternIndex); if (ch == '*') { //通配符星号*表示可以匹配任意多个字符 while (strIndex < strLength) { if (wildcardMatch(pattern.substring(patternIndex + 1), str.substring(strIndex))) { return true; } strIndex++; } } else if (ch == '?') { //通配符问号?表示匹配任意一个字符 strIndex++; if (strIndex > strLength) { //表示str中已经没有字符匹配?了。 return false; } } else { if ((strIndex >= strLength) || (ch != str.charAt(strIndex))) { return false; } strIndex++; } } return (strIndex == strLength); } public static void main(String[] paramert) { // 在此目录中找文件 String baseDIR = "d:/file"; // 找扩展名为txt的文件 String fileName = "*.txt"; List resultList = new ArrayList(); FileSearcher.findFiles(baseDIR, fileName, resultList); if (resultList.size() == 0) { System.out.println("No File Fount."); } else { for (int i = 0; i < resultList.size(); i++) { System.out.println(resultList.get(i));//显示查找结果。 } } } }
相关文章推荐
- Java递归搜索指定文件夹下的匹配文件
- Java递归搜索指定文件夹下的匹配文件
- Java递归搜索指定文件夹下的匹配文件
- Java递归搜索指定文件夹下的匹配文件
- Java递归搜索指定文件夹下的匹配文件
- Java递归搜索指定文件夹下的匹配文件
- Java递归搜索指定文件夹下的匹配文件
- java递归删除指定目录下的文件和文件夹
- java 递归实现删除或查询指定文件夹下的所有文件
- Java中如何利用File类递归的遍历指定目录中的所有文件和文件夹
- java正则匹配指定文件名称动态删除(保留最新的三个文件夹)升级版
- java io 使用递归删除指定的文件夹及其子文件夹和文件 先测试再正式使用哦
- Java读取文件夹下的指定类型的文件 递归筛选 包含子文件夹
- Java 查找指定文件夹下的匹配文件
- 指定文件目录下搜索关键字(Java实现,递归方式)
- java 从指定文件夹搜索符合条件的文件
- 多匹配递归删除指定文件/文件夹,用以简便备份你的VS.NET解决方案 (1:清理垃圾文件)
- 相当实用的——Java递归删除指定文件夹下的所有文件
- java基础之File对象中的FilenameFilter文件过滤以及递归列出指定文件夹路径下的所有文件和所有文件夹
- java读写文件-经过多方搜索整合成我需要的功能(读写指定文件夹下面的多个指定名称的txt中的内容全部写入到一个指定的txt中)