Simple File Searcher App: An Exercise in Concurrency

Here is a simple app for searching files on a computer. The app uses a recursive algorithm to compare names of files and folders with the search keyword and returns all the matches.

The procedure is as follows:

1.	list the roots of the filesystem
2.	for each root listed in step1:
	a.	list folders and files
	b.	for each file in a., compare keyword with file name.  
                If matched, list this file in results 
	c.	for each folder in a, 
		i.  compare keyword with folder name.  If matched, list this folder in results 
		ii. repeat 2a. for each folder
	d.	repeat a. to c. until no subfolders are found

The Java version is given below:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.filesearch.components;

import java.io.*;
import java.io.File.*;
import java.util.concurrent.*;
import java.util.Scanner;
/**
 *
 * @author Dell
 */
public class FileSearcherComponent implements Runnable{
    
    static String fileKeyword;
  
    static long count = 0;
  
    static File logFile;
  
    static BufferedWriter bw;
  
    private void logToFile(String path)
    {
  try{
    
    bw.write(path, 0, path.length());
    bw.newLine();
    
  }
  catch(IOException i)
  {
    i.printStackTrace();
  }
    }
    
    @Override
    public void run()
    {
  try{
      
    bw = new BufferedWriter(new FileWriter(logFile, true));
      
  }
  catch(IOException i)
  {
    i.printStackTrace();
  }
    
  long currTime = System.currentTimeMillis();
    
        File[] roots = File.listRoots(); //get drives
        
        for(File root:roots)
        {
            
            File rootFile = new File(root.getPath());
            
            traverseRoots(rootFile);
        }
    
  long endTime = System.currentTimeMillis();
    
  double seconds = (endTime - currTime) / 1000;
    
  System.out.printf("\n %d results in %f seconds.\n", count, seconds);
    
  try{
    
    bw.flush();
    
    bw.close();
  }
  catch(IOException i)
  {
    i.printStackTrace();
  }
    }
    
    private void traverseRoots(File root)
    {
        if(root.isDirectory())
        {
            
            String name = root.getName();

            if(name.contains(fileKeyword))
            {
                System.out.println(root.getAbsolutePath());
        
    logToFile(root.getAbsolutePath());
        
    count++;
            }
            
            File[] rootFolders = root.listFiles();

            if(rootFolders != null && rootFolders.length > 0){

                for(File rootFolder: rootFolders)
                {
                    if(rootFolder.isDirectory())
                    {
                        
                        String name1 = rootFolder.getName();

                        if(name1.contains(fileKeyword))
                        {
                            System.out.println(rootFolder.getAbsolutePath());
              
          logToFile(rootFolder.getAbsolutePath());
              
          count++;
                        }
                        
                        traverseRoots(rootFolder);
                    }
                    else if(rootFolder.isFile())
                    {
                        String name2 = rootFolder.getName();

                        if(name2.contains(fileKeyword))
                        {
                            System.out.println(rootFolder.getAbsolutePath());
              
          logToFile(rootFolder.getAbsolutePath());
              
          count++;
                        }
                    }
                    
                }
            }
        }
        else if(root.isFile())
        {
            String name3 = root.getName();

            if(name3.contains(fileKeyword))
            {
                System.out.println(root.getAbsolutePath());
        
    logToFile(root.getAbsolutePath());
        
    count++;
        
            }
        }
       
    }
    
    public static void main(String[] args)
    {
        FileSearcherComponent f = new FileSearcherComponent();
        
        ExecutorService es = Executors.newSingleThreadExecutor();
    
  logFile = new File("LOG.txt");
    
  if(logFile.exists())
  {
    logFile.delete();
  }
    
  try{
    
    logFile.createNewFile();
  }
  catch(IOException i)
  {
    i.printStackTrace();
  }
    
        System.out.print("Enter search keyword: ");
        
        fileKeyword  = new Scanner(System.in).nextLine();
    
        es.execute(f);	
    
    
    }
    
}

 

Copy, compile, run and ENJOY!

Leave a Reply

Your email address will not be published. Required fields are marked *