-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBruteForceSearch.java
More file actions
48 lines (39 loc) · 1.19 KB
/
BruteForceSearch.java
File metadata and controls
48 lines (39 loc) · 1.19 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
import java.io.*;
import java.util.*;
public class BruteForceSearch implements WordSearch{
private final String fileName;
private Map<String, Integer> result = new HashMap<String, Integer>();
private File file;
private int count =0;
public BruteForceSearch(File file) throws IOException {
this.file = file;
this.fileName = file.getName();
this.count = 0;
}
private void searchBruteForce(String toSearch) throws IOException {
FileInputStream fstream = new FileInputStream(file);
BufferedReader in = new BufferedReader(new InputStreamReader(fstream));
String readLine = "";
while ((readLine = in.readLine()) != null) {
String[] words = readLine.split("\\W");
for (String text : words) {
if (text.equalsIgnoreCase(toSearch)) {
count++;
}
}
}
in.close();
}
public String getFile() {
return fileName;
}
public int getCount(String word) {
return count;
}
public void search(String toSearch) throws IOException {
searchBruteForce(toSearch);
if (getCount(toSearch) != 0) {
System.out.println(getFile() + " - " + getCount(toSearch) + " matches ");
}
}
}