Java递归搜索指定文件夹下的匹配文件

  1. 原文链接:http://blog.csdn.net/jdsjlzx/article/details/6993316

  2. package com.lzx.file;  
  3.   
  4. import java.io.File;  
  5. import java.util.ArrayList;  
  6. import java.util.LinkedList;  
  7. import java.util.List;  
  8.   
  9. public class FileDemo07 {  
  10.   
  11.     public static void main(String[] args) {  
  12.          //    在此目录中找文件     
  13.         String baseDIR = "d:/temp";      
  14.         //    找扩展名为txt的文件     
  15.         String fileName = "*.txt";      
  16.         List resultList = new ArrayList();  
  17.         findFiles(baseDIR, fileName,resultList);      
  18.         if (resultList.size() == 0) {     
  19.             System.out.println("No File Fount.");     
  20.         } else {     
  21.             for (int i = 0; i < resultList.size(); i++) {     
  22.                 System.out.println(resultList.get(i));//显示查找结果。      
  23.             }     
  24.         }     
  25.   
  26.     }  
  27.   
  28.     /**   
  29.      * 递归查找文件   
  30.      * @param baseDirName  查找的文件夹路径   
  31.      * @param targetFileName  需要查找的文件名   
  32.      * @param fileList  查找到的文件集合   
  33.      */    
  34.     public static void findFiles(String baseDirName, String targetFileName, List fileList) {     
  35.         
  36.         File baseDir = new File(baseDirName);       // 创建一个File对象  
  37.         if (!baseDir.exists() || !baseDir.isDirectory()) {  // 判断目录是否存在  
  38.             System.out.println("文件查找失败:" + baseDirName + "不是一个目录!");  
  39.         }  
  40.         String tempName = null;     
  41.         //判断目录是否存在     
  42.         File tempFile;  
  43.         File[] files = baseDir.listFiles();  
  44.         for (int i = 0; i < files.length; i++) {  
  45.             tempFile = files[i];  
  46.             if(tempFile.isDirectory()){  
  47.                 findFiles(tempFile.getAbsolutePath(), targetFileName, fileList);  
  48.             }else if(tempFile.isFile()){  
  49.                 tempName = tempFile.getName();  
  50.                 if(wildcardMatch(targetFileName, tempName)){  
  51.                     // 匹配成功,将文件名添加到结果集  
  52.                     fileList.add(tempFile.getAbsoluteFile());  
  53.                 }  
  54.             }  
  55.         }  
  56.     }     
  57.          
  58.     /**   
  59.      * 通配符匹配   
  60.      * @param pattern    通配符模式   
  61.      * @param str    待匹配的字符串   
  62.      * @return    匹配成功则返回true,否则返回false   
  63.      */    
  64.     private static boolean wildcardMatch(String pattern, String str) {     
  65.         int patternLength = pattern.length();     
  66.         int strLength = str.length();     
  67.         int strIndex = 0;     
  68.         char ch;     
  69.         for (int patternIndex = 0; patternIndex < patternLength; patternIndex++) {     
  70.             ch = pattern.charAt(patternIndex);     
  71.             if (ch == '*') {     
  72.                 //通配符星号*表示可以匹配任意多个字符     
  73.                 while (strIndex < strLength) {     
  74.                     if (wildcardMatch(pattern.substring(patternIndex + 1),     
  75.                             str.substring(strIndex))) {     
  76.                         return true;     
  77.                     }     
  78.                     strIndex++;     
  79.                 }     
  80.             } else if (ch == '?') {     
  81.                 //通配符问号?表示匹配任意一个字符     
  82.                 strIndex++;     
  83.                 if (strIndex > strLength) {     
  84.                     //表示str中已经没有字符匹配?了。     
  85.                     return false;     
  86.                 }     
  87.             } else {     
  88.                 if ((strIndex >= strLength) || (ch != str.charAt(strIndex))) {     
  89.                     return false;     
  90.                 }     
  91.                 strIndex++;     
  92.             }     
  93.         }     
  94.         return (strIndex == strLength);     
  95.     }   

猜你喜欢

转载自blog.csdn.net/lmpt90/article/details/53363088
今日推荐