<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package twothreadsearch;


import java.util.*;
import java.io.*;

public class twothreadsearch  {
	
	

	static int filesearched = 0; // counts the files searched
    
    static LinkedList &lt;File&gt; data = new LinkedList &lt;File&gt;();
    
    static File getfile;
    
    static int size;
    
    static String newfilename;
    
    static int datasize;
    
    static File [] arraylist;
    
    static int filefound;
    
 
    
    
    public static void main(String[] args) throws IOException{
   	
   	
   	 boolean flag = true;  //set while loop condition to be true to start the program
   	 while (flag )
   	 {
           
       //    System.out.println("Search always start from root \n");
           Scanner scan = new Scanner (System.in); //set object scan to read user input
           File dir = new File("C:/"); // state the begin search path
         
           
           arraylist = dir.listFiles();// list all files in dir and assign it to array call arraylist
           
           datasize = arraylist.length;
           
           System.out.println("This directory contains " + datasize + " nodes. ");
           
           if (arraylist != null) //test if file in arraylist, if yes
           {
               System.out.println("Enter text to search in system: \n"); //prompt user input
               String fileName = scan.nextLine(); // scan in user input and assign it to object fileName
               System.out.println ("Start searching...");
               long startTime = System.currentTimeMillis(); // sets the start time
          	 
          
               newfilename = fileName.toLowerCase(); //call function to extract out extension
               
            

             search (dir, newfilename); // call function to search the directory for files
               
               size = data.size();
               
               //2 threads
           
                Thread thread1 = new Thread (new Runnable(){
                	public void run() {
                		for (int i = 0; i&lt; size/2 ; i++) {
                		
                	    	  try{
                                  getfile = data.get(i);
                                  searchword (getfile, newfilename);
                              }catch (FileNotFoundException e){}
                         
                	      }
                	}
                });   
                
              
             
                
                Thread thread2 = new Thread (new Runnable(){
                	public void run() {
                		 for  (int j = (int) Math.ceil(size/2) ; j &lt; size ; j++){
             		    	  try{
                                   getfile = data.get(j);
                                   searchword (getfile, newfilename);
                               }catch (FileNotFoundException e){}
                               
             		    
             		      }
                	}
                });
                
            
               		thread1.start();
                    		
                    thread2.start();

                    	
                    //end time
                    long endTime = System.currentTimeMillis(); // sets end time
                    
                    System.out.println (endTime);
                    
                
                    //substract start time from end time and calculate millis time to minutes
                    double mintime = endTime - startTime ;
                    // prints how long it took in minutes
                    System.out.println ("It took " + mintime +" millisecond ");
                
                //give user option if they want to run another test
                System.out.println ("Do you like to have another search? (yes/no)");
                String answer = scan.nextLine(); // scan in user input
                if  (answer.equals("yes")) //if is yes
                    flag = true; // set flag to true and loops the program
                else{
                flag = false; //if is no, then set the flag to false to end the program
                System.out.println ("Good Bye");
                }
            }}
     }
    
            
     private static synchronized void searchword (File pfile, String name) throws FileNotFoundException
     {
        try {
        		FileReader file = new FileReader (pfile);
        		BufferedReader br = new BufferedReader(file);
        		String line;
                while ((line = br.readLine())!= null)
                  {
                      //System.out.println(line);
                      StringTokenizer st = new StringTokenizer(line);
                      // It will go through the file and
                      //gives the number of tokens in the file
                    
                      String word;
                
                      while(st.hasMoreTokens())
                      {
                          word = st.nextToken();
                         // System.out.println(word);
                      
                          if ((word.toLowerCase()).compareTo(name)== 0)
                          {
                              System.out.println(line);
                              System.out.println("In directory : " + pfile.getCanonicalPath()); // prints out the path
                          }
                        }
                  }
                file.close();
                br.close();
            
            } catch (IOException e) {}
     }
        

    

    //recursive search function for directory within directory
 
    private static synchronized void search(File dir, String newfilename) throws IOException  {

        File[] files = dir.listFiles(); //list file in folder dir to array list files
    
        if (files != null)
        {
          for (int k =0 ; k&lt; files.length; k++) //loop through array and each entity assign to file call f
          {
            if (files[k].isDirectory()) // test if f is a directory
                search (files [k], newfilename); // recursively call function search
            else if (files[k]!= null)// if it's a file
            {
                    filesearched = filesearched + 1; // counts how many files search
                    if ((files [k].getName()).endsWith(".txt"))
                    
                    	data.add(files [k]);
            }
          }
        }
    }
}




            
    
     </pre></body></html>