java 使用java在目录中的文本文件中递归搜索?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1455383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Search recursively in text files in a directory with java?
提问by David Tinker
I need to search a text in all the files of a directory and subdirectorys any clever idea ?
我需要在一个目录和子目录的所有文件中搜索文本有什么好主意吗?
回答by Rich Seller
Use the org.apache.commons.io.FileUtils.iterateFiles()method from Apache commons-io
使用Apache commons-io 中的org.apache.commons.io.FileUtils.iterateFiles()方法
From the javadoc:
从javadoc:
Allows iteration over the files in a given directory (and optionally its subdirectories) which match an array of extensions.
允许迭代给定目录(以及可选的其子目录)中与扩展名数组匹配的文件。
For example:
例如:
Iterator iter = FileUtils.iterateFiles(directory,
new String[]{"txt","java"}, true);
while(iter.hasNext()) {
File file = (File) iter.next();
searchInFile(file);
}
回答by james
Runtime.getRuntime().exec("grep", "-r", <searchText>);
回答by David Tinker
Try a class with a recursive method something like this:
尝试使用递归方法的类,如下所示:
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
}
}
回答by ?MER TA?CI
this is my solution. It searches the "searchingText" string in a java project directory.I hope, this helps someone.
这是我的解决方案。它在 java 项目目录中搜索“searchingText”字符串。我希望这对某人有所帮助。
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();
}
}
}
}
}
回答by n00b
//edit u have to define CheckFile funct...
//编辑你必须定义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;
}

