搜尋檔案中的關鍵字

handawei_5發表於2010-10-18

首先2個思維:

     第一個是:藉助阻塞列表,有一個執行緒把一個資料夾下的所有檔案放入列表。

                    然後是100個執行緒,迴圈不斷的從列表中取出檔案,檢查這個檔案是否有關鍵字,有就計數加一,直到列表為空,返回這個執行緒的計數。整體來說就讀取就一百個執行緒,不斷檢查。

 

  第二個是:用到遞迴,一個callable執行緒,傳入一個檔案,如果這個檔案是個目錄,則把它目錄下的每個檔案傳入這個定義的callable執行緒,如果是個檔案 則搜尋關鍵字,最後返回這個執行緒的計數(目錄則返回目錄下的所有計數和,檔案就返回這個檔案的計數),感覺是樹形的結構。

貼程式碼:

第一個:

 

package v1ch14.FutureTest;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

/**
 * @version 2010-10-18 上午09:34:18
 * @author DaWei han
 * @EMAIL  handawei_1@126.com
 */
public class MyFutureTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		System.out.print("Enter the directory eg. D:\\Java\\jdk1.6\\src:");
	    String directory=in.nextLine();
	    System.out.println("Enter the key word eg. volatile:");
	    String keyword=in.nextLine();
	    System.out.println("finished the input.");
	    final int NUMBER_QUEUE_SIZE=10;
	    final int NUMBER_SEARCH=100;
	    
	    
	    
	    BlockingQueue<File> queue=new ArrayBlockingQueue<File>(NUMBER_QUEUE_SIZE);
	    new Thread(new FileEnumerationTask(queue,new File(directory))).start();
	    List<FutureTask<Integer>> rsList=new ArrayList<FutureTask<Integer>>();
	    
	    for(int i=0;i<NUMBER_SEARCH;i++)
	    {
	      	FutureTask<Integer> task=new FutureTask<Integer>(new SerachTack(queue,keyword));
	    	rsList.add(task);
	      	new Thread(task).start();
	      
	    }
	    int num=0;
	    for(FutureTask<Integer> taskTwo:rsList)
	    {
	    	try {
	    		System.out.println("the num is:"+num);
				num+=taskTwo.get();
				System.out.println("the num is:"+num);
				System.out.println("========================================>");
			} catch (Exception e) {
				e.printStackTrace();
			} 
	    }
	    System.out.println("the total num is :"+num);
	}
}

class SerachTack implements Callable<Integer>
{
	public SerachTack(BlockingQueue<File> queue,String keyword)
	{
		this.queue=queue;
		this.keyword=keyword;
	}
	public Integer call() throws Exception {
		rsNum=0;
        
        boolean done=false;
        
        while(!done)
        {
        	File file=queue.take();
        	if(file==FileEnumerationTask.DUMM)
        	{
        		done =true;
        		queue.put(file);
        	}else{
        		rsNum+=searchKey(file,keyword);
        	}
        }
		return rsNum;
	}
	private int searchKey(File file,String keyword)
	{
		//此為每行要是有關鍵字,就加1
//		int num=0;
//		try {
//			Scanner in=new Scanner(new FileInputStream(file));
//			while(in.hasNextLine())
//			{
//				if(in.nextLine().contentEquals(keyword)){
//					num++;
//				};
//			}
//		}catch (FileNotFoundException e) {
//			e.printStackTrace();
//		}
//	  return num;
	//=======>此為一個檔案要是有關鍵字 就返回1 @1
		try {
			Scanner in=new Scanner(new FileInputStream(file));
			while(in.hasNextLine())
			{
				if(in.nextLine().contains(keyword)){
					System.out.println("matched in the file:"+file.getPath());
					return 1;
				};
			}
		}catch(FileNotFoundException e) {
			e.printStackTrace();
		}
		return 0; 
  //<====================
	}

	private Integer rsNum;
	private BlockingQueue<File> queue;
	private String keyword;
}

class FileEnumerationTask implements Runnable
{
	public FileEnumerationTask(BlockingQueue<File> queue,File directory)
	{
		this.queue=queue;
		this.directory=directory;
	}
	public void run() {
		try {
		    searchFile(this.directory);
			queue.put(DUMM);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	public void searchFile(File directory)
	{
		try {
		File[] files=directory.listFiles();
		for(File file:files)
		{
			if(file.isDirectory())searchFile(file);
			else
			queue.put(file);
		}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	public static File DUMM=new File("");
	private File directory;
	private BlockingQueue<File> queue;
}

 

 

第二個:

 

package v1ch14.FutureTest;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

/**
 * @version 2010-10-18 下午02:36:52
 * @author DaWei han
 * @EMAIL  handawei_1@126.com
 */
public class MyFutureTest2 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		System.out.print("Enter the directory eg. D:\\Java\\jdk1.6\\src:");
	    String directory=in.nextLine();
	    System.out.println("Enter the key word eg. volatile:");
	    String keyword=in.nextLine();
	    System.out.println("finished the input.");
	    FutureTask<Integer> task=new FutureTask<Integer>(new MyMatchCounter(new File(directory), keyword));
	    new Thread(task).start();
	    try {
			System.out.println("the Toatl num is:"+task.get());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
class MyMatchCounter implements Callable<Integer>
{	
   public MyMatchCounter(File directory,String keyword)
   {
	   this.directory=directory;
	   this.keyword=keyword;
   }
   
	public Integer call() throws Exception {
       int num=0;
		List<FutureTask<Integer>> rsList=new ArrayList<FutureTask<Integer>>();
		if(directory.isDirectory())
        {
			File[] files=directory.listFiles();
			for(File file:files)
			{
				MyMatchCounter counter=new MyMatchCounter(file,this.keyword);
				FutureTask<Integer> task=new FutureTask<Integer>(counter);
				rsList.add(task);
			   new Thread(task).start();	
			}
        	
        }else{
         num=search(directory);	
        }
		for(FutureTask<Integer> taskTwo:rsList)
		{
			num+=taskTwo.get();
		}
		return num;
	}
	
  public Integer search(File file)
  {
	  try {
		Scanner in=new Scanner(new FileInputStream(file));
        while(in.hasNextLine())
        {
        	if(in.nextLine().contains(this.keyword))
        	{
        		System.out.println(" Match the keyword in file:"+file.getPath());
                return 1;        		
        	}
        }
	  } catch (FileNotFoundException e) {
		e.printStackTrace();
	}
	return 0;  
  }
private File directory;
private String keyword;
}

 

下面是用執行緒池改寫第二個:

 

package v1ch14.ThreadPoolTest;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @version 2010-10-18 下午04:04:31
 * @author DaWei han
 * @EMAIL  handawei_1@126.com
 */
public class MyThreadPoolTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		System.out.print("Enter the directory eg. D:\\Java\\jdk1.6\\src:");
	    String directory=in.nextLine();
	    System.out.print("Enter the key word eg. volatile:");
	    String keyword=in.nextLine();
	    System.out.print("finished the input.");

		ExecutorService pool=Executors.newCachedThreadPool();
		MyMatchCounter counter=new MyMatchCounter(new File(directory),keyword,pool);
        Future<Integer> result= pool.submit(counter);	
		
        try {
			System.out.println("the total number is :"+result.get());
		}catch (Exception e) {
			e.printStackTrace();
		} 
		pool.shutdown();
	}
	

}
class MyMatchCounter implements Callable<Integer>
{
	public MyMatchCounter(File direcotry,String keyword,ExecutorService pool)
	{
		this.directory=direcotry;
		this.keyword=keyword;
		this.pool=pool;
	}
	public Integer call() throws Exception {
         int num=0;
         ArrayList<Future<Integer>> results = new ArrayList<Future<Integer>>();
		File[] files=directory.listFiles();
		for(File file:files)
		{
			if(file.isDirectory())
			{
				results.add(pool.submit(new MyMatchCounter(file, keyword, pool)));
			}else
			{
			   num+=searchFile(file);	
			}
		}
		for(Future<Integer> rs:results)
		{
			num+=rs.get();
		}
		return num;
	}
private Integer searchFile(File file)
{
	
	Scanner in=null;
      try {
    	  in= new Scanner(new FileInputStream(file));
		while(in.hasNextLine())
		{
			if(in.nextLine().contains(this.keyword))
				return 1;
		}
	} catch (Exception e) {
		e.printStackTrace();
	}finally{
		in.close();
	}
	return 0;
}
 private File directory;
 private String keyword;
 private ExecutorService pool;
}

 

相關文章