forked from java8/Java8InAction
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilesearch.java
More file actions
80 lines (63 loc) · 2.52 KB
/
filesearch.java
File metadata and controls
80 lines (63 loc) · 2.52 KB
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
import java.io.*;
import java.util.concurrent.*;
import java.util.*;
import java.nio.file.*;
public class WordSearchMultithreading {
// ExecutorService for managing threads
private static final ExecutorService executor = Executors.newFixedThreadPool(4);
public static void main(String[] args) {
if (args.length < 2) {
System.out.println("Usage: java WordSearchMultithreading <directory> <word>");
System.exit(1);
}
String directoryPath = args[0];
String wordToSearch = args[1];
try {
int totalOccurrences = searchWordInDirectory(directoryPath, wordToSearch);
System.out.println("Total occurrences of '" + wordToSearch + "': " + totalOccurrences);
} catch (Exception e) {
System.err.println("Error occurred: " + e.getMessage());
} finally {
executor.shutdown();
}
}
private static int searchWordInDirectory(String directoryPath, String word) throws Exception {
File directory = new File(directoryPath);
if (!directory.exists() || !directory.isDirectory()) {
throw new IllegalArgumentException("Invalid directory path: " + directoryPath);
}
// List to hold Future objects for each file task
List<Future<Integer>> futures = new ArrayList<>();
// Recursively process all files and subdirectories
Files.walk(Paths.get(directoryPath)).filter(Files::isRegularFile).forEach(filePath -> {
futures.add(executor.submit(() -> countWordInFile(filePath.toFile(), word)));
});
int totalOccurrences = 0;
// Aggregate results from all threads
for (Future<Integer> future : futures) {
totalOccurrences += future.get();
}
return totalOccurrences;
}
private static int countWordInFile(File file, String word) {
int count = 0;
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line;
while ((line = reader.readLine()) != null) {
count += countOccurrences(line, word);
}
} catch (IOException e) {
System.err.println("Error reading file: " + file.getName());
}
return count;
}
private static int countOccurrences(String line, String word) {
int count = 0;
int index = 0;
while ((index = line.indexOf(word, index)) != -1) {
count++;
index += word.length();
}
return count;
}
}