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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 * 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 *