1. 程式人生 > >java通過萬用字元查詢檔案

java通過萬用字元查詢檔案

package com.ym.test.file;

import java.io.*; 
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class FileDirectorySearch {
	/**
	 * 查詢指定目錄下的所有檔案
	 * @param args
	 */ 
	public static void main(String[] args) { 
//	   // TODO Auto-generated method stub
		FileDirectorySearch star=new FileDirectorySearch(); 
//	   String path="d:"; 
//	   File war=new File(path); 
//	   int count=star.findTxtFileCount(war,"exe"); 
//	   System.out.println("----: " + count);
		
		File[] files = star.getFiles("d:/share", "*");
		for(int i=0;i<files.length;i++){
			System.out.println(files[i]);
		}
		
	} 
	/**
	 * 建立查詢指定目錄下檔案的方法
	 * 
	 * @param allList
	 *            指定目錄
	 * @param endName
	 *            指定以“”結尾的檔案
	 * @return 得到的檔案數目
	 */ 
	public int findTxtFileCount(File allList,String endName){ 
	   // 
	   int textCount=0; 
	// 建立fileArray名字的陣列
	File[] fileArray= allList.listFiles(); 
	// 如果傳進來一個以檔案作為物件的allList 返回0
	if(null==fileArray){ 
	     return 0; 
	    } 
	// 偏歷目錄下的檔案
	for(int i=0;i<fileArray.length;i++){ 
	   // 如果是個目錄
	       if(fileArray[i].isDirectory()){ 
	     // System.out.println("目錄: "+fileArray[i].getAbsolutePath());
	        // 遞迴呼叫
	        textCount+=findTxtFileCount(fileArray[i].getAbsoluteFile(),endName); 
	     // 如果是檔案
	      }else if(fileArray[i].isFile()){ 
	     // 如果是以“”結尾的檔案
	     if(fileArray[i].getName().endsWith(endName)){ 
	        // 展示檔案
	        System.out.println("exe檔案: "+fileArray[i].getAbsolutePath()); 
	        // 所有以“”結尾的檔案相加
	        textCount++; 
	     } 
	     } 
	} 
	return textCount; 

	} 
	 /**
	    * 在本資料夾下查詢
	    * @param s String 檔名
	    * @return File[] 找到的檔案
	    */
	   public static File[] getFiles(String s)
	   {
	     return getFiles("./",s);
	   }
	  
	   /**
	    * 獲取檔案
	    * 可以根據正則表示式查詢
	    * @param dir String 資料夾名稱
	    * @param s String 查詢檔名,可帶*.?進行模糊查詢
	    * @return File[] 找到的檔案
	    */
	   public static File[] getFiles(String dir,String s) {
	     //開始的資料夾
	     File file = new File(dir);

	     s = s.replace('.', '#');
	     s = s.replaceAll("#", "\\\\.");
	     s = s.replace('*', '#');
	     s = s.replaceAll("#", ".*");
	     s = s.replace('?', '#');
	     s = s.replaceAll("#", ".?");
	     s = "^" + s + "$";

	     System.out.println(s);
	     Pattern p = Pattern.compile(s);
	     ArrayList list = filePattern(file, p);

	     File[] rtn = new File[list.size()];
	     list.toArray(rtn);
	     return rtn;
	   }

	   /**
	    * @param file File 起始資料夾
	    * @param p Pattern 匹配型別
	    * @return ArrayList 其資料夾下的資料夾
	    */

	   private static ArrayList filePattern(File file, Pattern p) {
	     if (file == null) {
	       return null;
	     }
	     else if (file.isFile()) {
	       Matcher fMatcher = p.matcher(file.getName());
	       if (fMatcher.matches()) {
	         ArrayList list = new ArrayList();
	         list.add(file);
	         return list;
	       }
	     }
	     else if (file.isDirectory()) {
	       File[] files = file.listFiles();
	       if (files != null && files.length > 0) {
	         ArrayList list = new ArrayList();
	         for (int i = 0; i < files.length; i++) {
	           ArrayList rlist = filePattern(files[i], p);
	           if (rlist != null) {
	             list.addAll(rlist);
	           }
	         }
	         return list;
	       }
	     }
	     return null;
	   }



}