-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIOSearcher.java
More file actions
32 lines (26 loc) · 1.01 KB
/
Copy pathIOSearcher.java
File metadata and controls
32 lines (26 loc) · 1.01 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
package test;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class IOSearcher {
// Private constructor to prevent instantiation
private IOSearcher() {}
public static boolean search(String word, String... fileNames) {
// Iterate over each file name
for (String fileName : fileNames) {
try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
String line;
// Read each line from the file
while ((line = reader.readLine()) != null) {
// Check if the line contains the word
if (line.contains(word)) {
return true; // If word found in any file, return true
}
}
} catch (IOException e) {
e.printStackTrace(); // Handle or log the exception
}
}
return false; // Word not found in any file
}
}