我想在java中编写一个程序,在我的代码中使用eclipse搜索插件搜索整个工作区中的字符串。 我已经搜索过这个问题,但无法找到结果。 请帮助。 每个建议都表示赞赏。 提前谢谢。
答案 0 :(得分:1)
您确实意识到Eclipse中已经存在这样的功能,对吗?在Eclipse Search
- > File...
输入您的文字,选择*.*
作为文件名模式,将workspace
选为范围。
或者您是否想要编写一个内部使用此搜索的插件?
从您的评论中我得到的图片是您正在寻找代码来遍历目录/文件并搜索它们。也许this question可以帮助你。
答案 1 :(得分:0)
使用以下内容来递归文件的所有项目和文件夹:
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
// Get all the projects in the workspace
IProject [] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
// Search each project
for (IProject project : projects)
{
searchContainer(project);
}
/*
* Search a project or folder.
*/
private void searchContainer(IContainer container)
{
// Get all the resources in the container
IResource [] members = container.members();
// Look at each resource
for (IResource member : members)
{
if (member instanceof IContainer)
{
// Resource is a folder, search that
searchContainer((IContainer)member);
}
else if (member instanceof IFile)
{
// Resource is a file, search that
searchFile((IFile)member);
}
}
}
/*
* Search a file.
*/
private void searchFile(IFile file)
{
// TODO search the file
}
您应该在Job
或其他后台任务中运行此操作,因为它可能需要一段时间才能运行。
答案 2 :(得分:0)
包search_workspace09;
import java.io。*;
import java.util.regex。*;
public class workspace_search09 {
public static void main(String args[])
{
try
{
String dirName="D:/test_folder";
String stringsearch="world";
//String fileName = "test.txt";
File dir = new File(dirName);
// Open the file c:\test.txt as a buffered reader
File[] dirs=dir.listFiles();
if(dirs!=null)
{
for (int i=0;i<dirs.length;i++)
{
if(dirs[i].isFile())
{
File filename=dirs[i];
System.out.println("Files to search in " +filename);
BufferedReader bf=new BufferedReader(new FileReader(filename));
//System.out.println(filename);
// Start a line count and declare a string to hold our current line.
int countline=0;
String line;
//pattern search
Pattern p = Pattern.compile("\\b"+stringsearch+"\\b", Pattern.CASE_INSENSITIVE);
System.out.println("Search Criteria " +" Filename\t\t\t"+ " Line no. " + " Position\t" + " Line Text ");
while ( (line = bf.readLine()) != null)
{
// Increment the count and find the index of the word
countline++;
Matcher m = p.matcher(line);
// indicate all matches on the line
while(m.find())
{
System.out.println(stringsearch +"\t\t"+ filename+"\t\t" + countline + "\t " + m.start() + "\t"+line);
}
}
//continue;
}
}}}
catch (IOException e)
{
System.out.println("IO Error Occurred: " + e.toString());
}}}
答案 3 :(得分:0)
public class workspace_search14 {
public static void main(String[] args)throws IOException
{
String dir="D:/test_folder";
File root=new File(dir);
findFiles(root,0);
}
public static void findFiles(File root,int depth) throws IOException
{
File[] listOfFiles = root.listFiles();
for (int i = 0; i < listOfFiles.length; i++)
{
String iName = listOfFiles[i].getName();
if (listOfFiles[i].isFile())
{
if (iName.endsWith(".txt") || iName.endsWith(".TXT")||iName.endsWith(".java"))
{
for (int j = 0; j < depth; j++)
System.out.print("\t");
System.out.println("\nFile_Name: "+iName);
searchFiles(listOfFiles[i]);
}
}
else if (listOfFiles[i].isDirectory())
{
for (int j = 0; j < depth; j++)
System.out.print("\t");
System.out.println("\nDirectory_Name: "+iName);
findFiles(listOfFiles[i], depth+1);
}
}
}
public static void searchFiles(File FileName)throws IOException
{
int countline=0;
String line;
String stringsearch="world";
BufferedReader bf=new BufferedReader(new FileReader(FileName));
Pattern p = Pattern.compile("\\b"+stringsearch+"\\b", Pattern.CASE_INSENSITIVE);
System.out.println("Search Criteria " +" Filename\t\t\t"+ " Line no. " + " Position\t" + " Line Text ");
while ( (line = bf.readLine()) != null)
{
countline++;
Matcher m = p.matcher(line);
while(m.find())
{
System.out.println(stringsearch +"\t\t"+ FileName+"\t\t" + countline + "\t " + m.start() + "\t"+line);
}}}}