A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© lvwangxiao 中级黑马   /  2016-3-19 22:24  /  495 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. import java.awt.event.ActionEvent;
  2. import java.awt.event.ActionListener;
  3. import java.io.File;

  4. import javax.swing.JButton;
  5. import javax.swing.JFrame;
  6. import javax.swing.JLabel;
  7. import javax.swing.JOptionPane;
  8. import javax.swing.JScrollPane;
  9. import javax.swing.JTextArea;
  10. import javax.swing.JTextField;
  11. import javax.swing.WindowConstants;


  12. public class FileSearchFrame extends JFrame{

  13.         //添加标签
  14.         private JLabel lab1 = new JLabel("目录:");
  15.         //路径文本框
  16.         private JTextField txtPath = new JTextField();
  17.        
  18.         //标签
  19.         private JLabel lab2 = new JLabel("关键字:");
  20.         //关键字文本框
  21.         private JTextField txtKey = new JTextField();
  22.        
  23.         //结果标签
  24.         //按钮
  25.         private JLabel labResult = new JLabel();
  26.         JButton butSearch = new JButton("搜索");
  27.        
  28.         //文本域
  29.         JTextArea txtResult = new JTextArea();
  30.        
  31.         //定义文件/文件夹记数字
  32.         int dirCount = 0;
  33.         int fileCount = 0;
  34.        
  35.         public FileSearchFrame(){
  36.                 init();
  37.                 addComponent();
  38.         }
  39.        
  40.         private void init(){
  41.                
  42.                 //设置窗体大小不可变
  43.                 this.setResizable(false);
  44.                 //设置大小
  45.                 this.setSize(650,440);
  46.                 //设置标题
  47.                 this.setTitle("文件搜索");
  48.                 //设置居中
  49.                 FrameTools.centerFrame(this);
  50.                 //设置布局
  51.                 this.setLayout(null);
  52.                 //设置关闭动作
  53.                 this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
  54.         }
  55.        
  56.         private void addComponent() {

  57.                 lab1.setBounds(30, 30, 80, 25);
  58.                 this.add(lab1);
  59.                
  60.                 txtPath.setBounds(70, 30, 300, 25);
  61.                 this.add(txtPath);

  62.                 lab2.setBounds(390, 30, 80, 25);
  63.                 this.add(lab2);

  64.                
  65.                 txtKey.setBounds(440, 30, 100, 25);
  66.                 this.add(txtKey);

  67.                 butSearch.setBounds(570, 30, 60, 25);
  68.                 butSearch.addActionListener(new ActionListener(){
  69.                         @Override
  70.                         public void actionPerformed(ActionEvent e) {
  71.                                 butSearchClick();
  72.                         }
  73.                 });
  74.                 this.add(butSearch);
  75.                 //JScrollPane
  76.                 JScrollPane scrollPane = new JScrollPane();
  77.                 scrollPane.setBounds(30, 70, 600, 300);
  78.                 //搜索结果文本域
  79.                 scrollPane.setViewportView(txtResult);
  80.                 this.add(scrollPane);
  81.                
  82.                 //标签
  83.                 this.labResult.setBounds(30, 380, 600, 25);
  84.                 this.add(labResult);
  85.                
  86.         }
  87.        
  88.         //点击搜索按钮方法
  89.         private void butSearchClick(){
  90.                 //获取目录
  91.                 String dirPath = txtPath.getText();
  92.                 File dir = new File(dirPath);
  93.                
  94.                 //判断目录是否存在
  95.                 if(!dir.exists()){
  96.                         //为用户提示您输入的文件夹不存在,请仔细检查
  97.                         JOptionPane.showMessageDialog(this, "您输入的文件夹不存在,请仔细检查","信息有误",JOptionPane.ERROR_MESSAGE);
  98.                         return;
  99.                 }
  100.                
  101.                 //判断是否是文件夹
  102.                 if(!dir.isDirectory()){
  103.                         //为用户提示您输入的不是文件夹,请仔细检查
  104.                         JOptionPane.showMessageDialog(this, "您输入的不是文件夹,请仔细检查!","信息有误",JOptionPane.ERROR_MESSAGE);
  105.                         return;
  106.                 }
  107.                
  108.                 //获取关键字
  109.                 String key = txtKey.getText();
  110.                 //判断关键字是否已输入
  111.                 if(key.trim().equals("")){
  112.                         //为用户提示请输入关键字
  113.                        
  114.                         JOptionPane.showMessageDialog(this, "请输入关键字!","信息有误",JOptionPane.ERROR_MESSAGE);
  115.                         return;
  116.                 }
  117.                
  118.                 //调用方法,将指定文件夹中包含关键字的文件或文件夹放到主窗口文本域
  119.                 searchFile(dir,key);
  120.                
  121.                 //设置结果JLabel
  122.                 labResult.setText("目录:" + dirCount + "个    文件:" + fileCount + "个");
  123.                
  124.         }
  125.             //搜索文件--递归方法
  126.                 //将指定文件夹中包含关键字的文件或文件夹放到主窗口文本域
  127.                 //将符合条件的文件或文件夹个数进行统计
  128.        
  129.         private void searchFile(File dir, String key){
  130.                
  131.                 //获取该文件夹下所有的文件对象(文件夹与文件)
  132.                 File[] filesorDirs = dir.listFiles();
  133.                
  134.                 //遍历数组,依次获取每一个文件对象
  135.                 for(File fileorDir : filesorDirs){
  136.                         //如果是文件
  137.                         if(fileorDir.isFile()){
  138.                                 //进行文件处理
  139.                                 //获取文件名
  140.                                 String fileName = fileorDir.getName();
  141.                                 //判断文件名是否包含
  142.                                 if(fileName.contains(key)){
  143.                                         //将符合条件的文件数+1
  144.                                         fileCount++;
  145.                                         //如果包含,将文件的绝对路径打印到文本域
  146.                                         String fileAbsPath = fileorDir.getAbsolutePath();
  147.                                         txtResult.append(fileAbsPath+"\r\n");
  148.                                        
  149.                                 }
  150.                         }
  151.                        
  152.                         //如果是文件夹
  153.                         if(fileorDir.isDirectory()){
  154.                                 //判断文件名是否包含
  155.                                 if(fileorDir.getName().contains(key)){
  156.                                         //将符合条件的文件夹数+1
  157.                                         dirCount++;
  158.                                         //如果包含,就将文件夹的绝对路径打印到文本域
  159.                                         String fileAbsPath = fileorDir.getAbsolutePath();
  160.                                         //向文本域中追加内容
  161.                                         txtResult.append(fileAbsPath + "\r\n");
  162.                                 }
  163.                                
  164.                                 //进行文件夹处理
  165.                                 searchFile(fileorDir,key);
  166.                                
  167.                         }
  168.                 }
  169.                
  170.         }
  171.        
  172.                
  173.         public static void main(String[] args) {
  174.                 new FileSearchFrame().setVisible(true);
  175.         }
  176.        
  177.                
  178. }
复制代码

1 个回复

倒序浏览
顶  。。。。。。。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马