java從一個目錄中找到字串在那個文件中出現

職業驅魔師發表於2013-07-29
package MgUtil.Util;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.regex.Pattern;

import com.zdyn.test2.XMArray;
public class DIRUtil {
    static XMArray xm=new XMArray();
    public static XMArray getAllFile(String strPath) {
        File dir = new File(strPath);
        File[] files = dir.listFiles();
        if (files == null)
            return xm;
        for (int i = 0; i < files.length; i++) {
            if (files[i].isDirectory()) {
                getAllFile(files[i].getAbsolutePath());
            } else {
                String strFileName = files[i].getAbsolutePath();
                File f=new File(strFileName);
                xm.push(f);
                //System.out.println("---" + strFileName);
            }
        }
        return xm;
    };
    
    static void findStrFromFiles(File f,String str0){
        try{
        //System.out.println("正在從"+f.getAbsolutePath()+"中查詢....");
        FileReader fr=new FileReader(f);
         BufferedReader bw = new BufferedReader(fr);
         String str="";
         String str2="";
         int i=0;
         while((str2=bw.readLine())!=null){
             if(str2.indexOf(str0)!=-1){
                 System.out.println(f.getAbsolutePath());
                 System.out.println("第"+i+"行");
                 break;
             }
             i++;
         }
        
        }catch(Exception e){
            
        }
    }
    
    
    static void findInAllFile(XMArray xm2,String str){
        for(int i=0;i<xm2.length();i++){
            File f=(File)xm2.get(i);
            findStrFromFiles(f,str);
        }
    }
    
    static void FindInAllFile2(String url,String str){
        XMArray xm=getAllFile(url);
        findInAllFile(xm,str);
    }
    public static void main(String[] args) {
//        XMArray xm=getAllFile("D:\\project\\SRC\\src");
//        findInAllFile(xm,"JdbcUrl");
//        System.out.println(xm.length());
        FindInAllFile2("D:\\project\\SRC\\src", "JdbcUrl");
    }
}



相關文章