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


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

相关内容