搜尋目錄下所有檔案內容是否包含指定的關鍵詞
import java.util.*;
import java.io.*;
import java.util.concurrent.*;
/**
* Created by QYT on 2017/8/2.
*/
public class BlockingQueueTest {
private static final int FILE_QUEUE_SIZE=10;
private static final int SEARCH_THREADS=100;
private static final File DUMMY=new File("");
private static BlockingQueue<File> queue=new ArrayBlockingQueue<>(FILE_QUEUE_SIZE);
public static void main(String[] args) {
try(Scanner in=new Scanner(System.in)){
System.out.print("Enter base directory(e.g. /opt/jdk1.8.0/src): ");
String directory=in.nextLine();
System.out.print("Enter keyword(e.g. volatile): ");
String keyword=in.nextLine();
Runnable enumerator=()->{
try{
enumerate(new File(directory));
queue.put(DUMMY);
}catch (InterruptedException e){}
};
new Thread(enumerator).start();
for (int i = 0; i < SEARCH_THREADS; i++) {
Runnable searcher=()->{
try {
boolean done=false;
while (!done){
File file=queue.take();
if(file == DUMMY){
queue.put(file);
done =true;
}else {
search(file,keyword);
}
}catch (Exception e){}
new Thread(searcher).start();
public static void enumerate(File directory) throws InterruptedException {
File[] files=directory.listFiles();
for(File f:files){
if(f.isDirectory())enumerate(f);
else queue.put(f);
public static void search(File file,String keyword) throws FileNotFoundException {
try(Scanner in=new Scanner(file,"UTF-8") ){
int lineNumber=0;
while (in.hasNextLine()){
lineNumber++;
String line=in.nextLine();
if(line.contains(keyword))
System.out.printf("%s:%d:%s%n",file.getPath(),lineNumber,line);
---------------------
給你原始碼 自己可以做一個
搜尋目錄下所有檔案內容是否包含指定的關鍵詞
import java.util.*;
import java.io.*;
import java.util.concurrent.*;
/**
* Created by QYT on 2017/8/2.
*/
public class BlockingQueueTest {
private static final int FILE_QUEUE_SIZE=10;
private static final int SEARCH_THREADS=100;
private static final File DUMMY=new File("");
private static BlockingQueue<File> queue=new ArrayBlockingQueue<>(FILE_QUEUE_SIZE);
public static void main(String[] args) {
try(Scanner in=new Scanner(System.in)){
System.out.print("Enter base directory(e.g. /opt/jdk1.8.0/src): ");
String directory=in.nextLine();
System.out.print("Enter keyword(e.g. volatile): ");
String keyword=in.nextLine();
Runnable enumerator=()->{
try{
enumerate(new File(directory));
queue.put(DUMMY);
}catch (InterruptedException e){}
};
new Thread(enumerator).start();
for (int i = 0; i < SEARCH_THREADS; i++) {
Runnable searcher=()->{
try {
boolean done=false;
while (!done){
File file=queue.take();
if(file == DUMMY){
queue.put(file);
done =true;
}else {
search(file,keyword);
}
}
}catch (Exception e){}
};
new Thread(searcher).start();
}
}
}
public static void enumerate(File directory) throws InterruptedException {
File[] files=directory.listFiles();
for(File f:files){
if(f.isDirectory())enumerate(f);
else queue.put(f);
}
}
public static void search(File file,String keyword) throws FileNotFoundException {
try(Scanner in=new Scanner(file,"UTF-8") ){
int lineNumber=0;
while (in.hasNextLine()){
lineNumber++;
String line=in.nextLine();
if(line.contains(keyword))
System.out.printf("%s:%d:%s%n",file.getPath(),lineNumber,line);
}
}
}
}
---------------------
給你原始碼 自己可以做一個