java通过线程获得路径_java线程池 多线程 搜索包含关键字的文件路径

packageorg.jimmy.searchfile20180807.ui;importjava.awt.Dimension;importjava.awt.Toolkit;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.io.File;importjava.io.FileOutputStream;importjava.io.IOException;importjava.util.concurrent.LinkedBlockingQueue;importjava.util.concurrent.ThreadPoolExecutor;importjava.util.concurrent.TimeUnit;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JPanel;importjavax.swing.JScrollPane;importjavax.swing.JTextArea;importjavax.swing.SwingUtilities;importorg.jimmy.searchfile20180807.main.SearchMain;importorg.jimmy.searchfile20180807.main.ThreadMain;public classSearchUi {public static voidmain(String[] args){

SwingUtilities.invokeLater(newRunnable() {

@Overridepublic voidrun() {

SearchUi searchUi= newSearchUi();

searchUi.init();

}

});

}public voidinit(){

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= newJPanel();

panel.setLayout(null);

panel.setBounds(0, 0, (int) dimension.getWidth(), (int) dimension.getHeight());//添加滚动条

JScrollPane scrollPane = newJScrollPane(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 = newJTextArea();

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 = newJTextArea();

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 = newJTextArea();

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(newActionListener() {

@Overridepublic voidactionPerformed(ActionEvent e) {

String searchDirPath= searchDirPathText.getText().replaceAll(" ", "");

String searchedFilePath= searchedFilePathText.getText().replaceAll(" ", "");

File searchedFile= newFile(searchedFilePath);

FileOutputStream fos= null;try{

fos= newFileOutputStream(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= newString[]{ keyWordsStr };

}

LinkedBlockingQueue workQueue = new LinkedBlockingQueue();

ThreadPoolExecutor executor= new ThreadPoolExecutor(3, 100, 3600, TimeUnit.SECONDS, workQueue);for(int i = 0; i < keyWordsArr.length; i++){

String keyWords=keyWordsArr[i];

ThreadMain threadMain= newThreadMain(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);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值