使用java开发文本全局搜索器
2015-12-15 19:49
375 查看
ps:推荐大家使用windows builder进行可视化编程。链接:WindowBuilder Pro Eclipse Update Site - http://download.eclipse.org/windowbuilder/WB/release/R201506241200-1/4.4/
下面是文本全局搜索器的代码:
下面是文本全局搜索器的代码:
package test; import java.awt.Button; import java.awt.EventQueue; import java.awt.TextArea; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JTextField; import java.awt.Font; public class Main { private JFrame frame; private JTextField textField; private JTextField textField_1; private TextArea textArea; private String filePath; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { Main window = new Main(); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the application. */ public Main() { initialize(); } /** * Initialize the contents of the frame. */ private void initialize() { frame = new JFrame(); frame.setTitle("\u6587\u672C\u5168\u5C40\u641C\u7D22\u795E\u5668"); frame.setResizable(false); frame.setBounds(100, 100, 531, 402); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(null); textField = new JTextField(); textField.setBounds(10, 10, 368, 21); frame.getContentPane().add(textField); textField.setColumns(25); textField_1 = new JTextField(); textField_1.setBounds(10, 41, 368, 21); frame.getContentPane().add(textField_1); textField_1.setColumns(25); textArea = new TextArea(); textArea.setFont(new Font("华文楷体", Font.PLAIN, 14)); textArea.setBounds(10, 68, 495, 295); frame.getContentPane().add(textArea); Button button = new Button("\u9009\u62E9\u6587\u4EF6\uFF08\u5939\uFF09"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JFileChooser jf = new JFileChooser(); jf.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); jf.showDialog(null, null); File file = jf.getSelectedFile(); try { filePath = file.getAbsolutePath(); } catch (Exception e2) { } textField.setText(filePath); } }); button.setBounds(384, 8, 121, 23); frame.getContentPane().add(button); Button button_1 = new Button("\u5F00\u59CB\u641C\u7D22"); button_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String keyWord = textField_1.getText().trim().toString(); if (keyWord == null || keyWord.equals("")) { JOptionPane.showMessageDialog(null, "你还未输入搜索内容", "温馨提示", JOptionPane.INFORMATION_MESSAGE); return; } filePath = textField.getText().trim().toString(); // textArea.setText("测试"+filePath); MySearchThread thread = new MySearchThread(filePath, keyWord); thread.start(); } }); button_1.setBounds(384, 39, 121, 23); frame.getContentPane().add(button_1); } /** * 文件搜索线程类 * * @author shiweixian * */ class MySearchThread extends Thread { String filePath; String keyWord; List<File> list ; public MySearchThread(String filePath, String keyWord) { this.filePath = filePath; this.keyWord = keyWord; list = new ArrayList<>(); } @Override public void run() { if (filePath == null || filePath.equals("")) { JOptionPane.showMessageDialog(null, "无法找到该文件,请重新操作", "温馨提示", JOptionPane.INFORMATION_MESSAGE); return; } File file = new File(filePath); if (file.isFile()) { ScannerFile(file); //搜索文件里的内容 } else if(file.isDirectory()){ try { getFilePath(file); //获得文件夹下的所有文件 } catch (NullPointerException e) { JOptionPane.showMessageDialog(null, "路径不正确", "温馨提示", JOptionPane.INFORMATION_MESSAGE); } if(list != null){ for(int i = 0;i<list.size();i++){ textArea.append(list.get(i).getAbsolutePath()+":\n"); ScannerFile(list.get(i)); } textArea.append("\r\n"); } } } /** * 扫描文件,搜索出想要的内容 */ private void ScannerFile(File file){ try { BufferedReader reader = new BufferedReader(new FileReader(file)); String temp; int count = 1; while ((temp = reader.readLine()) != null) { if (temp.contains(keyWord)) textArea.append("\t第" + count + "行: " + temp + "\n"); count++; } reader.close(); } catch (FileNotFoundException e) { JOptionPane.showMessageDialog(null, "无法找到该文件,请重新操作", "温馨提示", JOptionPane.INFORMATION_MESSAGE); return; } catch (IOException e) { JOptionPane.showMessageDialog(null, "很抱歉,搜索过程中出现错误", "错误", JOptionPane.INFORMATION_MESSAGE); return; } } /** * 获取文件夹下所有子文件的路径(递归) * @param file */ private void getFilePath(File file){ if(file.isFile()) list.add(file); else if(file.isDirectory()){ File[] files = file.listFiles(); for(int i =0;i<files.length;i++){ getFilePath(files[i]); } } } } }
相关文章推荐
- JAVA设计模式初探之装饰者模式
- JAVA设计模式初探之组合模式
- JAVA设计模式初探之适配器模式
- java 垃圾回收机制
- Java中LOCK和synchronized区别
- JAVA设计模式初探之桥接模式
- java集合——树集(TreeSet)+对象的比较
- java进阶(一)--控制反转IoC
- spring boot日志配置
- Maven出现 Unable to locate the Javac Compiler in解决方法
- java注解
- JavaWeb自学之html学习笔记 Day-2
- Java学习----方法的重载
- springMvc配置mongo数据库
- spring boot配置文件
- Java API —— ArrayList类 & Vector类 & LinkList类
- Java把内存划分
- JAVA中使用mybatis配置数据库
- Java学习----构造方法的重载
- JAVA基本类型取值范围