Skip to content

Commit 4b7a49d

Browse files
committed
Chapter 18 new Exercises
1 parent a896482 commit 4b7a49d

File tree

4 files changed

+98
-1
lines changed

4 files changed

+98
-1
lines changed

ch_18/Exercise18_30.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public static void main(String[] args) {
4444
}
4545

4646
static void countOccurrences(File[] files, String word, int fileIndex) throws IOException {
47-
if (files.length - 1 == fileIndex) {
47+
if (files.length == fileIndex) {
4848
return;
4949
}
5050
if (files[fileIndex].isFile()) {
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package ch_18.exercise18_31;
2+
3+
import java.io.*;
4+
import java.util.List;
5+
import java.util.stream.Collectors;
6+
7+
/**
8+
* 18.31 (Replace words) Write a program that replaces all occurrences of a word with a
9+
* new word in all the files under a directory, recursively. Pass the parameters from
10+
* the command line as follows:
11+
* <p>
12+
* java Exercise18_31 dirName oldWord newWord
13+
* <p>
14+
* Example cmd:
15+
* javac Exercise18_31.java
16+
* java Exercise18_31 ch_18/exercise18_31/TestDirectory the bologna
17+
*/
18+
public class Exercise18_31 {
19+
public static void main(String[] args) {
20+
if (args.length < 2) {
21+
System.err.println("Usage: java Exercise18_30 dirName word");
22+
System.exit(0);
23+
}
24+
25+
String directory = args[0];
26+
String oldWord = args[1];
27+
String newWord = args[2];
28+
29+
File dir = new File(directory);
30+
31+
if (dir.isDirectory()) {
32+
File[] files = dir.listFiles();
33+
if (files != null && files.length > 0) {
34+
try {
35+
replaceWords(files, oldWord, newWord, 0);
36+
} catch (IOException ioException) {
37+
ioException.printStackTrace();
38+
}
39+
}
40+
41+
} else {
42+
System.out.println("Please specify a Directory for 'dirName'. ");
43+
}
44+
System.out.println("The word: \"" + oldWord + "\" has been replaced with \"" + newWord + "\" for files in " +
45+
"directory: " + dir.getName());
46+
47+
}
48+
49+
static void replaceWords(File[] files, String oldWord, String newWord, int fileIndex) throws IOException {
50+
if (files.length == fileIndex) {
51+
return;
52+
}
53+
runReplace(oldWord, newWord, files[fileIndex]);
54+
replaceWords(files, oldWord, newWord, fileIndex + 1); // Recurse
55+
}
56+
57+
/* Method to perform replacement logic */
58+
static void runReplace(String oldWord, String newWord, File file) throws IOException {
59+
if (file.isFile()) {
60+
FileReader fileReader = new FileReader(file);
61+
BufferedReader bufferedReader = new BufferedReader(fileReader);
62+
List<String> resultList = bufferedReader.lines()
63+
.map(s -> s.replace(oldWord, newWord))
64+
.collect(Collectors.toList());
65+
66+
bufferedReader.close();
67+
FileWriter fileWriter = new FileWriter(file);
68+
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
69+
for (String line : resultList) {
70+
bufferedWriter.write(line);
71+
bufferedWriter.newLine();
72+
}
73+
bufferedWriter.close();
74+
}
75+
76+
}
77+
78+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Elon Musk has an enormous rocket and a tiny house. Last year the multibillionaire
2+
(who is the second-richest person in the world) announced that he was going to sell off most of his physical possessions,
3+
including multiple mansions, and “will own no house”. He appears to have made good on much of that pledge and is now living in a $50,000,
4+
375-square-foot, prefab home in Texas that he rents from his company SpaceX.
5+
To say that Musk has “fans” is an understatement.
6+
He has devotees. The 50-year-old has an army of admirers, most of whom are young and male,
7+
who seem to believe that he is the second coming. Predictably, Musk’s new living arrangements have sparked an orgy of admiration from the MuskBros,
8+
who see it as another example of how their dear leader is selflessly – and modestly – saving the world.
9+
“Reminder: Elon Musk is the richest person globally with a net worth of $193B, and he lives in a $50k foldable home that he rents …
10+
” one guy gushed in a tweet that went viral. “Stop buying stuff you don’t need to impress people you don’t like.”
11+
Now that last bit is very good advice, I’ll grant you that.
12+
But let’s not pretend Musk is living a simple and sustainable lifestyle, shall we?
13+
His new digs aren’t so much an actual home as a $50,000 office; I’m fairly confident he’s not entertaining his six
14+
children there, anyway. It’s easy to eschew possessions when you can afford to get your hands on whatever you want,
15+
whenever you want it. And while Musk may not technically own a home, his company effectively owns the village of
16+
Boca Chica where his house is located. SpaceX has been accused of ushering out the village’s residents, many of whom are retirees,
17+
and pressuring them to sell their homes. Public beaches are reportedly frequently closed with little warning when SpaceX is running tests.
18+
Musk has also taken the liberty of unofficially renaming Boca Chica “Starbase”. “For better or worse, Boca Chica belongs to Elon now,” TexasMonthly sighed.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
What happened to America’s concern about Afghan women?A humanitarian crisis is unfolding in Afghanistan and women and children will disproportionately suffer.“That’s not America’s problem,” Joe Biden has basically said. For 20 years bologna US has made a big song and dance about how it was largely inAfghanistan to liberate Afghan women and girls. This was always disingenuous, of course, but bologna US’s disastrously abrupt exit from bolognacountry underscores just how disingenuous it was. US involvement in Afghanistan has been a litany of disasters and wasnever going to end well. But Biden could certainly have tried to end it a little more thoughtfully, and less abruptly, than he did.

0 commit comments

Comments
 (0)