使用java在目录中的文本文件中递归搜索?

时间:2009-09-21 16:18:18

标签: java search file

我需要在目录和子目录的所有文件中搜索任何一个聪明的想法吗?

5 个答案:

答案 0 :(得分:18)

使用Apache commons-io

中的org.apache.commons.io.FileUtils.iterateFiles()方法

来自javadoc:

  

允许迭代给定目录(以及可选的子目录)中与扩展数组匹配的文件。

例如:

Iterator iter =  FileUtils.iterateFiles(directory, 
    new String[]{"txt","java"}, true);

while(iter.hasNext()) {
    File file = (File) iter.next();
    searchInFile(file);
}

答案 1 :(得分:4)

尝试使用类似这样的递归方法的类:

public class Searcher {

    private String toFind, toReplace;
    // whatever other info you need

    public void searchInTextFiles(File dir) {
        File[] a = dir.list();
        for (File f : a) {
            if (f.isDirectory()) {
                searchInTextFiles(f);
            } else if (f.name().endsWith(".txt")) {
                searchInFile(f);
            }
        }
    }

    private void searchInFile(File f) {
        // do whatever you need to do on f using fields from this class
    }
}

答案 2 :(得分:4)

Runtime.getRuntime().exec("grep", "-r", <searchText>);

答案 3 :(得分:1)

这是我的解决方案。它搜索&#34; searchingText&#34; java项目目录中的字符串。我希望,这有助于某人。

package textSearch;

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.sql.Connection;
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.List;
    import java.util.Scanner;

    import org.apache.commons.io.FileUtils;
    import org.apache.commons.io.filefilter.DirectoryFileFilter;
    import org.apache.commons.io.filefilter.RegexFileFilter;
    import org.apache.commons.io.filefilter.TrueFileFilter;


    public class TextSearchingInFilesInDirectory {

        private static String filePath ="";
        static int fileCount = 0;   
        static String fileName ;
        static int lineNumber=0;

        private static final String folderPath = "C:\\Users\\omertasci\\EclipseWorkspace\\AnyProjectName\\src";

        public static void main(String[] args) throws FileNotFoundException{

        File dir = new File(folderPath);
        List<File> files =(List<File>) FileUtils.listFiles( dir, TrueFileFilter.INSTANCE, DirectoryFileFilter.DIRECTORY);

        for (File file : files) {
            try {
                System.out.println(file.getCanonicalPath()+ " is scanned.");
                filePath=file.getCanonicalPath();

                if (file.isFile()) {
                    //System.out.println(file.getName());
                    fileName=file.getName();
                    try {
                        FileReader reader = new FileReader(filePath );
                        BufferedReader br = new BufferedReader(reader); 
                        String s; 
                        while((s = br.readLine()) != null) { 
                            lineNumber++;
                            if(s.contains("searchingText")){  // you can change searchText with any keyword .
                                    System.out.println("searchingText is found in "+ file.getName()+ " at "+lineNumber+". line.\n"+ "----"+s.trim()+ "----");
                                                    }                           
                        } 
                        reader.close(); 
                    }
                    catch(Exception e){
                        e.printStackTrace();
                    }
                    }

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
}

}

答案 4 :(得分:0)

//编辑你必须定义CheckFile功能...

void Found(String dir,Vector flist){
File srchdir(dir);
File[] listf=listFiles();
    for(int i=0;i<srchdir.listf.length;i++){
        if(listf[i].isDirectory()){ Found(dir+"/"+listf[i].getName(),flist); 
        }else{ if(CheckFile(listf[i])) flist.add(dir+"/"+listf[i].getName()); }
    }
}


Vector Found(String dir){
File srchdir(dir);
Vector flist=new Vector();
File[] listf=listFiles();
    for(int i=0;i<srchdir.listf.length;i++){
        if(listf[i].isDirectory()){ Found(dir+"/"+listf[i].getName(),flist); 
        }else{ if(CheckFile(listf[i])) flist.add(dir+"/"+listf[i].getName()); }
    } 
    return flist;
}