自动搜索指定指定目录的头文件中的特定方法

import java.util.*;

import javax.swing.JTextArea;

import java.io.File;
import java.io.FilenameFilter;
//created by yuanxinying
//date 2019/9/13
//function automacially search for the key word from the .h file in given directory
public class HiJava implements FilenameFilter {

//真的是久违的java 语句啊~  I am so sorry for not using you again
//terriably sorry for this 
String result;
public HiJava() {
	result="";//初始化result
}
public void SearchMethod(String keyword, File dir) {
	//output1 function definition 
	//output2 directory+filename
	//first stage 递归的打开我们的folder,然后搜索.h文件,如果本文件夹里没有.h文件则退出
	File[] temp_dir = dir.listFiles();
	try {
		for(int i=0;i<temp_dir.length;i++) {
			
			if(temp_dir[i].isDirectory()) {
				//System.out.println(temp_dir[i]);
				SearchMethod(keyword,temp_dir[i]);
			}
		}
		
		//开始文件检查
		String[] filename = dir.list(this);
		for(String str : filename) {
			//System.out.println(dir +"\\"+ str);
			//matching 
			File input=new File(dir+"\\"+str);
			Scanner scan = new Scanner(input,"utf-8");
			int line=0;// line number
			while(scan.hasNext()) {
				String s=scan.nextLine();
				line++;
				if(s.contains(keyword)) {
					//下一步待改进,匹配方法以及枚举类型的输出格式。
					//System.out.println(input.getPath()+" line num:"+line);
					//System.out.println(s);
					result+=input.getPath()+" line num:"+line+"\n";
					result+=s+"\n";
				}
			}
		}
		
	}catch(Exception e) {
		
		System.out.println("something wrong in SearchMethod");
		e.printStackTrace();
	}
	
}
 //主要是直接显示我们的方法的定义
public String Search(String keyword, File dir) {
	SearchMethod(keyword, dir);
	if(result=="") {
		return "error";
	}
	return result;
}
@Override
public boolean accept(File f, String name) {
	return name.endsWith(".h");
}

}

import javax.swing.;
import java.awt.
;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
//author : yuanxinying
//create date 2019/9/8 23:00
//function show 2 testinput 1 testoutput and 1 button and get the input from the users
public class Graphic extends JFrame {
JPanel jp1, jp2, jp3,jp4;
JLabel jlb1, jlb2;
JButton jsearchbtn;
JTextField jkeyword;
JTextField jdirpath;
JTextArea jresult;
//构造方法
public Graphic(){
//创建组件
jp1 = new JPanel();
jp2 = new JPanel();
jp3 = new JPanel();
jp4 = new JPanel();

	jlb1 = new JLabel("关  键  字:");
	jlb2 = new JLabel("搜索路径:");
	
	jsearchbtn = new JButton("搜索");
	//为btn添加点击事件
	jsearchbtn.addActionListener(
			new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					// TODO Auto-generated method stub
					HiJava test=new HiJava();
					File dir=new File(getSearchDir());
					String name = getKeyWord();
					 System.out.println(name);
					 System.out.println(dir.getPath());
					String result=test.Search(name, dir);
					showResult(result);
				}
			}
			);
	
	jkeyword = new JTextField(30);
	jdirpath = new JTextField(30);
	jresult  = new JTextArea(40,40);
	//设置布局管理器
	setLayout(new GridLayout(4, 1));
	
	//加入组件
	jp1.add(jlb1);
	jp1.add(jkeyword);
	jp2.add(jlb2);
	jp2.add(jdirpath);
	jp3.add(jresult);
	jp4.add(jsearchbtn);
	//加入到JFrame
	add(jp1);
	add(jp2);
	add(jp3);
	add(jp4);
	//设置窗体
	setTitle("yxy小工具");
	setSize(500, 200);
	setResizable(false);
	setLocation(100,100);
	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	setVisible(true);
}	

public String getKeyWord() {

  if(jkeyword.getText()=="") {
	  return "error";
   }
  return jkeyword.getText() ;

}

public String getSearchDir() {
if(jdirpath.getText()=="") {
return “error”;
}
return jdirpath.getText() ;
}

public void showResult(String res) {
//通过追加的方式来呈现我们的搜索结果
System.out.println(res);
jresult.append(res);
//实时显示我们添加的文件内容
jresult.paintImmediately(jresult.getBounds());
}
public static void main(String[] args) {
new Graphic();
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yxy1124260794

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值