java线程池 多线程 搜索包含关键字的文件路径
package org.jimmy.searchfile20180807.main;
public class ThreadMain implements Runnable{
private int taskNum;
private String searchDirPath;
private String keyWords;
public ThreadMain(int taskNum, String searchDirPath, String keyWords){
this.taskNum = taskNum;
this.searchDirPath = searchDirPath;
this.keyWords = keyWords;
}
@Override
public void run() {
System.out.println("正在执行task " + taskNum);
System.out.println("当前关键字:" + keyWords);
SearchMain searchMain = new SearchMain(searchDirPath, keyWords);
searchMain.search();
System.out.println("task " + taskNum + "执行完毕");
}
}
package org.jimmy.searchfile20180807.main; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream; public class SearchMain { private String searchDirPath;
private String keyWords; public SearchMain(String searchDirPath, String keyWords){
this.searchDirPath = searchDirPath;
this.keyWords = keyWords;
} //计算文件数量
public static int count = 0; public static FileOutputStream fos = null; /**
* Author: Yuxin.Yang(Jimmy)
* Time: 2018年8月7日 上午9:02:45
* Detail: 查询包含关键字的文件的路径
*/
public void search(){
File file = new File(searchDirPath);
File[] files = file.listFiles();
getFiles(files);
System.out.println("count:" + count);
} //递归搜索文件并写入搜索到的路径到文件
public void getFiles(File[] files){
FileInputStream fis = null;
try{
for(File file : files){
count++;
if(file.isDirectory()){
getFiles(file.listFiles());
}else{
StringBuffer sb = new StringBuffer();
byte[] bytes = new byte[1024];
fis = new FileInputStream(file);
int len = 0;
while((len = fis.read(bytes)) != -1){
sb.append(new String(bytes, 0, len, "utf-8"));
}
fis.close();
if(sb.indexOf(keyWords) >= 0){
System.out.println("包含关键字(" + keyWords + ")的文件路径:" + file.getAbsolutePath());
fos.write(("包含关键字(" + keyWords + ")的文件路径:" + file.getAbsolutePath() + System.lineSeparator()).getBytes());
fos.flush();
}
}
}
}catch(Exception e){
e.printStackTrace();
}
} }
package org.jimmy.searchfile20180807.ui; import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit; import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities; import org.jimmy.searchfile20180807.main.SearchMain;
import org.jimmy.searchfile20180807.main.ThreadMain; public class SearchUi { public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
SearchUi searchUi = new SearchUi();
searchUi.init();
}
}); } public void init(){
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("搜索文件路径");
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
frame.setBounds(0, 0, (int) dimension.getWidth(), (int) dimension.getHeight());
frame.setLayout(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); int scrollPaneWidth = (int) dimension.getWidth() - 15;
int scrollPaneHeight = (int) dimension.getHeight() - 35;
JPanel panel = new JPanel();
panel.setLayout(null);
panel.setBounds(0, 0, (int) dimension.getWidth(), (int) dimension.getHeight());
//添加滚动条
JScrollPane scrollPane = new JScrollPane(panel);
scrollPane.setBounds(0, 0, scrollPaneWidth, scrollPaneHeight);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
frame.add(scrollPane); //初始坐标
int x = 0;
int y = 0; //加上输入要搜索的文件夹路径的提示文本
int height = 30;
JLabel searchDirPathLabel = new JLabel("请输入要搜索的文件夹的路径:");
searchDirPathLabel.setBounds(x, y, scrollPaneWidth, height);
searchDirPathLabel.setHorizontalAlignment(JLabel.CENTER);
panel.add(searchDirPathLabel); //加上输入要搜索的文件夹路径的文本框
y += height + 20;
height = 100;
final JTextArea searchDirPathText = new JTextArea();
searchDirPathText.setBounds(x, y, scrollPaneWidth, height);
panel.add(searchDirPathText); //加上输入路径的文本框的提示文本
y += height + 20;
height = 30;
JLabel searchedFilePathLabel = new JLabel("请输入生成的搜索结果文件的路径:");
searchedFilePathLabel.setBounds(x, y, scrollPaneWidth, height);
searchedFilePathLabel.setHorizontalAlignment(JLabel.CENTER);
panel.add(searchedFilePathLabel); //加上输入路径的文本框
y += height + 20;
height = 100;
final JTextArea searchedFilePathText = new JTextArea();
searchedFilePathText.setBounds(x, y, scrollPaneWidth, height);
panel.add(searchedFilePathText); //加上输入关键字的文本框的提示文本
y += height + 20;
height = 30;
JLabel keyWordsLabel = new JLabel("请输入需要搜索的关键字(如果有多个,用英文半角逗号隔开):");
keyWordsLabel.setBounds(x, y, scrollPaneWidth, height);
keyWordsLabel.setHorizontalAlignment(JLabel.CENTER);
panel.add(keyWordsLabel); //加上输入关键字的文本框
y += height + 20;
height = 100;
final JTextArea keyWordsText = new JTextArea();
keyWordsText.setBounds(x, y, scrollPaneWidth, height);
panel.add(keyWordsText); //加上搜索按钮
y += height + 20;
height = 30;
int searchBtnWidth = 100;
JButton searchBtn = new JButton("搜索");
searchBtn.setBounds((scrollPaneWidth - searchBtnWidth) / 2, y, searchBtnWidth, height);
searchBtn.setHorizontalAlignment(JButton.CENTER);
searchBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String searchDirPath = searchDirPathText.getText().replaceAll(" ", "");
String searchedFilePath = searchedFilePathText.getText().replaceAll(" ", "");
File searchedFile = new File(searchedFilePath);
FileOutputStream fos = null;
try{
fos = new FileOutputStream(searchedFile);
if(!searchedFile.exists()){
searchedFile.createNewFile();
}
SearchMain.fos = fos;
}catch(Exception ex){
ex.printStackTrace();
}
String keyWordsStr = keyWordsText.getText();
String[] keyWordsArr = null;
if(keyWordsStr.indexOf(",") > 0){
keyWordsArr = keyWordsStr.split(",");
}else{
keyWordsArr = new String[]{ keyWordsStr };
}
LinkedBlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<Runnable>();
ThreadPoolExecutor executor = new ThreadPoolExecutor(3, 100, 3600, TimeUnit.SECONDS, workQueue);
for(int i = 0; i < keyWordsArr.length; i++){
String keyWords = keyWordsArr[i];
ThreadMain threadMain = new ThreadMain(i, searchDirPath, keyWords);
executor.execute(threadMain);
}
if(executor.isTerminated() && SearchMain.fos != null){
try {
SearchMain.fos.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
});
panel.add(searchBtn); //需要手动设置宽度高度(鼠标操作)
//frame.pack();
frame.setVisible(true);
} }
这次改了一个很关键的参数.

之前这里没加编码格式,结果用中文搜索在MyEclipse中可以搜索出来,打成jar包或exe之后搜索不出来.
原因:
因为MyEclipse我设置了默认的编码格式是utf-8.
java线程池 多线程 搜索包含关键字的文件路径的更多相关文章
- java线程池 多线程搜索文件包含关键字所在的文件路径
文件读取和操作类 import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; publi ...
- Ubuntu 查找文件夹中内容包含关键字的文件,路径为当前文件夹
From CSDN http://blog.csdn.net/lizhenmingdirk/article/details/44834997 grep -rl "keyword" ...
- Java线程和多线程(十二)——线程池基础
Java 线程池管理多个工作线程,其中包含了一个队列,包含着所有等待被执行的任务.开发者可以通过使用ThreadPoolExecutor来在Java中创建线程池. 线程池是Java中多线程的一个重要概 ...
- Java线程状态、线程start方法源码、多线程、Java线程池、如何停止一个线程
下面将依次介绍: 1. 线程状态.Java线程状态和线程池状态 2. start方法源码 3. 什么是线程池? 4. 线程池的工作原理和使用线程池的好处 5. ThreadPoolExecutor中的 ...
- Java多线程和并发(十二),Java线程池
目录 1.利用Executors创建线程的五种不同方式 2.为什么要使用线程池 3.Executor的框架 4.J.U.C的三个Executor接口 5.ThreadPoolExecutor 6.线程 ...
- 【Java 多线程】Java线程池类ThreadPoolExecutor、ScheduledThreadPoolExecutor及Executors工厂类
Java中的线程池类有两个,分别是:ThreadPoolExecutor和ScheduledThreadPoolExecutor,这两个类都继承自ExecutorService.利用这两个类,可以创建 ...
- Java线程池的几种实现 及 常见问题讲解
工作中,经常会涉及到线程.比如有些任务,经常会交与线程去异步执行.抑或服务端程序为每个请求单独建立一个线程处理任务.线程之外的,比如我们用的数据库连接.这些创建销毁或者打开关闭的操作,非常影响系统性能 ...
- Java线程池的原理及几类线程池的介绍
刚刚研究了一下线程池,如果有不足之处,请大家不吝赐教,大家共同学习.共同交流. 在什么情况下使用线程池? 单个任务处理的时间比较短 将需处理的任务的数量大 使用线程池的好处: 减少在创建和销毁线程上所 ...
- Java线程与多线程教程
本文由 ImportNew - liken 翻译自 Journaldev. Java线程是执行某些任务的轻量级进程.Java通过Thread类提供多线程支持,应用可以创建并发执行的多个线程. 应用 ...
随机推荐
- C++在循环内和循环外定义变量的差异
原文:http://blog.csdn.net/cashey1991/article/details/45127561 最后总结: 对于使用int等基本数据类型作为循环变量,只要你用的优化方面足够给力 ...
- 牛客练习赛13D:幸运数字Ⅳ(康托展开) F:关键字排序
链接:https://www.nowcoder.com/acm/contest/70/D 题目: 定义一个数字为幸运数字当且仅当它的所有数位都是4或者7. 比如说,47.744.4都是幸运数字而5.1 ...
- poj1419 求最大独立集
题目链接:http://poj.org/problem?id=1419 题意:求最大独立集 思路: 这里有一个定理: 最大独立集=补图的最大团最大团=补图的最大独立集 所以这里我们只要求给出的图的最大 ...
- linux修改用户主目录的方法 (转载)
转自:http://xiaomaimai.blog.51cto.com/1182965/274002 第一:修改/etc/passwd文件第二:usermod命令 详细说明如下:第一种方法:vi /e ...
- C# a标签请求下载文件
服务器文件后台处理方式: a标签: <a href="/FileUpload/DownloadFile?file=/UploadFiles/File/bfcd676b-13a8-419 ...
- bzoj 1699: [Usaco2007 Jan]Balanced Lineup排队【st表||线段树】
要求区间取min和max,可以用st表或线段树维护 st表 #include<iostream> #include<cstdio> using namespace std; c ...
- poj 2398 Toy Storage【二分+叉积】
二分点所在区域,叉积判断左右 #include<iostream> #include<cstdio> #include<cstring> #include<a ...
- Luogu P1134 阶乘问题 【数学/乱搞】 By cellur925
输入输出格式 输入格式: 仅一行包含一个正整数 NN . 输出格式: 一个整数,表示最右边的非零位的值. 输入输出样例 输入样例#1: 12 输出样例#1: 6 说明 USACO Training S ...
- python中threading模块中的Join类
join类是threading中用于堵塞当前主线程的类,其作用是阻止全部的线程继续运行,直到被调用的线程执行完毕或者超时.具体代码如下: import threading,time def doWai ...
- 使用了eclipse10年之后,我终于投向了IDEA
使用了eclipse10年之后,我终于投向了IDEA 最近,改用了idea,同事都说我投敌了.当然,这些同事都是和我一样的"老"程序员.不说毕业生,公司里的90后基本电脑都不会安装 ...