在给定的某个文件夹下查找给定的关键字keywords
程序源于Java核心技术(卷I)第8版 基础知识(中文版)
作者:Cay S.Horstmann,Gray Cornell ,
第14章多线程中的第14.6节阻塞队列,源码如下:
package v1ch14.BlockingQueueTest;
import java.io.*;
import java.util.*;
import java.util.concurrent.*;
/**
* @version 1.0 2004-08-01
* @author Cay Horstmann
*/
public class BlockingQueueTest
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter base directory (e.g. /usr/local/jdk1.6.0/src): ");
String directory = in.nextLine();
System.out.println("************ directory = "+directory);
System.out.print("Enter keyword (e.g. volatile): ");
String keyword = in.nextLine();
System.out.println("************ keyword = "+keyword);
final int FILE_QUEUE_SIZE = 10;
final int SEARCH_THREADS = 100;
BlockingQueue<File> queue = new ArrayBlockingQueue<File>(FILE_QUEUE_SIZE);
FileEnumerationTask enumerator = new FileEnumerationTask(queue, new File(directory));
new Thread(enumerator).start();
for (int i = 1; i <= SEARCH_THREADS; i++)
new Thread(new SearchTask(queue, keyword)).start();
}
}
/**
* This task enumerates all files in a directory and its subdirectories.
*/
class FileEnumerationTask implements Runnable
{
/**
* Constructs a FileEnumerationTask.
* @param queue the blocking queue to which the enumerated files are added
* @param startingDirectory the directory in which to start the enumeration
*/
public FileEnumerationTask(BlockingQueue<File> queue, File startingDirectory)
{
this.queue = queue;
this.startingDirectory = startingDirectory;
}
public void run()
{
try
{
enumerate(startingDirectory);
queue.put(DUMMY);
}
catch (InterruptedException e)
{
}
}
/**
* Recursively enumerates all files in a given directory and its subdirectories
* @param directory the directory in which to start
*/
public void enumerate(File directory) throws InterruptedException
{
File[] files = directory.listFiles();
for (File file : files)
{
if (file.isDirectory()) enumerate(file);
else queue.put(file);
}
}
public static File DUMMY = new File("");
private BlockingQueue<File> queue;
private File startingDirectory;
}
/**
* This task searches files for a given keyword.
*/
class SearchTask implements Runnable
{
/**
* Constructs a SearchTask.
* @param queue the queue from which to take files
* @param keyword the keyword to look for
*/
public SearchTask(BlockingQueue<File> queue, String keyword)
{
this.queue = queue;
this.keyword = keyword;
}
public void run()
{
try
{
boolean done = false;
while (!done)
{
File file = queue.take();
if (file == FileEnumerationTask.DUMMY)
{
queue.put(file);
done = true;
}
else search(file);
}
}
catch (IOException e)
{
e.printStackTrace();
}
catch (InterruptedException e)
{
}
}
/**
* Searches a file for a given keyword and prints all matching lines.
* @param file the file to search
*/
public void search(File file) throws IOException
{
Scanner in = new Scanner(new FileInputStream(file));
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);
}
in.close();
}
private BlockingQueue<File> queue;
private String keyword;
}
运行如下:
Enter base directory (e.g. /usr/local/jdk1.6.0/src): D:\all_install\MyEclipse_install\Workspace1\tesst\src\v1ch14
************ directory = D:\all_install\MyEclipse_install\Workspace1\tesst\src\v1ch14
Enter keyword (e.g. volatile): public
************ keyword = public
D:\all_install\MyEclipse_install\Workspace1\tesst\src\v1ch14\AlgorithmAnimation\AlgorithmAnimation.java:15:public class AlgorithmAnimation
D:\all_install\MyEclipse_install\Workspace1\tesst\src\v1ch14\AlgorithmAnimation\AlgorithmAnimation.java:17: public static void main(String[] args)
D:\all_install\MyEclipse_install\Workspace1\tesst\src\v1ch14\AlgorithmAnimation\AlgorithmAnimation.java:21: public void run()
D:\all_install\MyEclipse_install\Workspace1\tesst\src\v1ch14\AlgorithmAnimation\AlgorithmAnimation.java:37: public AnimationFrame()
D:\all_install\MyEclipse_install\Workspace1\tesst\src\v1ch14\AlgorithmAnimation\AlgorithmAnimation.java:47: public void actionPerformed(ActionEvent event)
D:\all_install\MyEclipse_install\Workspace1\tesst\src\v1ch14\UnsynchBankTest\UnsynchBankTest.java:8:public class UnsynchBankTest
............部分结果
看过代码后,对查找某文件夹下的文件中的关键字,有了一定的思路,如下:
1.查找给定文件夹下的所有文件,并放入队列中
2.根据输入的关键字,循环查找某个文件中包含的关键字行
代码通过测试,毕竟是大牛写的,我们小辈只有学习的份,可以直接copy,运行,祝大家好运!