*
* 运用递归算法,查找目录中文件名的程序
*
*/
/**
* @author 王洋(060619班陈斌整理)
*/
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.io.*;
public class FindString1 extends JFrame implements ActionListener, Runnable {
JTextField t_mulu = new JTextField();
JTextField t_neirong = new JTextField();
JTextArea t_ing = new JTextArea();
JTextArea t_end = new JTextArea();
FindString1() {
//设置窗体属性
this.setSize(400, 300);
//定义一大堆组件
JLabel l_mulu = new JLabel("请输入查找目录:");
JLabel l_neirong = new JLabel("请输入查找内容");
JButton b_liulan = new JButton("浏览");
JButton b_chazhao = new JButton("查找");
//加入滚动条
JScrollPane s_1 = new JScrollPane(t_ing);
JScrollPane s_2 = new JScrollPane(t_end);
//注册事件监听
b_chazhao.addActionListener(this);
//布置输入面板
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(2, 3));
p1.add(l_mulu);
p1.add(t_mulu);
p1.add(b_liulan);
p1.add(l_neirong);
p1.add(t_neirong);
p1.add(b_chazhao);
//布置结果面板
JPanel p2 = new JPanel();
p2.setLayout(new GridLayout(1, 2, 4, 5));
p2.add(s_1);
p2.add(s_2);
//布置窗体
this.setLayout(new BorderLayout());
this.add(p1, BorderLayout.NORTH);
this.add(p2, BorderLayout.CENTER);
}
public static void main(String[] args) {
FindString1 w = new FindString1();
w.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
System.out.println("ggggggggggggggg") ;
if (e.getActionCommand().equals("查找")) {
//得到用户输入的要查找的内容
t_ing.setText("");
t_end.setText("");
Thread t = new Thread(this);
t.start();
}
}
public void run() {
File f = new File(t_mulu.getText());
finding(f);
}
// 寻找指定目录以下的内容
public void find(File f) {
try {
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
String tmp;
//br.readLine()一行行的读
while ((tmp = br.readLine()) != null) {
if (tmp.indexOf(t_neirong.getText()) > -1) {
t_end.append(f + "/n");
return;
}
}
/**
* if(f.toString().indexOf(nerrong)>0) { t_end.append(f+"/n"); }
*/
} catch (Exception e) {
}
}
//查找指定目录的内容
public void finding(File f) {
try {
File[] f1 = f.listFiles();
for (int i = 0; i < f1.length; i++) {
if (f1[i].isDirectory()) {
//继续查找
finding(f1[i]);
} else {
//显示在t_ing的框里
t_ing.setText(f1[i] + t_ing.getText() + "/n");
//t_ing.append(f1[i] + "/n");
find(f1[i]);
}
}
} catch (Exception e) {
}
}
}