运用递归算法,查找目录中文件名的程序

 * 创建日期 2006-7-3
 *
 * 运用递归算法,查找目录中文件名的程序
 *
 */

/**
 * @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) {

  }

 }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值