forked from HarryDulaney/intro-to-java-programming
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExercise18_30.java
65 lines (54 loc) · 1.9 KB
/
Exercise18_30.java
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
package ch_18;
import java.io.*;
/**
* 18.30 (Find words) Write a program that finds all occurrences of a word in all the files
* under a directory, recursively.
* Pass the parameters from the command line as follows:
* <p>
* java Exercise18_30 dirName word
* For Example:
* cmd:>> javac Exercise18_30.java
* >>>>>> java Exercise18_30 ch_14 static
*/
public class Exercise18_30 {
static int occurrences = 0;
public static void main(String[] args) {
if (args.length < 2) {
System.err.println("Usage: java Exercise18_30 dirName word");
System.exit(0);
}
String directory = args[0];
String word = args[1];
File dir = new File(directory);
if (dir.isDirectory()) {
File[] files = dir.listFiles();
if (files != null && files.length > 0) {
try {
wordSearch(files, word, 0);
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
} else {
System.out.println("Please specify a Directory for 'dirName'. ");
}
System.out.println("The word: \"" + word + "\" occurs " + occurrences + " times in: " + dir.getName());
}
static void wordSearch(File[] files, String word, int fileIndex) throws IOException {
/* Recursion Stopping condition */
if (files.length == fileIndex) {
return;
}
if (files[fileIndex].isFile()) {
FileReader fileReader = new FileReader(files[fileIndex]);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line;
while ((line = bufferedReader.readLine()) != null) {
if (line.contains(word)) {
occurrences++;
}
}
}
wordSearch(files, word, fileIndex + 1);
}
}