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
public class MarkdownDemo {
public static void main(String[] args) throws IOException {
String blogPath = "D:\\data\\clawhub";
String oldDomain = "https://cdn.jsdelivr.net/gh";
String newDomain = "https://xxxx";
execute(Paths.get(blogPath), oldDomain, newDomain);
}

private static void execute(Path blogPath, String oldDomain, String newDomain) {
try (Stream<Path> stream = Files.list(blogPath)) {
stream.forEach((Path path) -> {
if (Files.isDirectory(path)) {
execute(path, oldDomain, newDomain);
} else {
replace(path, oldDomain, newDomain);
}
});
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private static void replace(Path path, String oldDomain, String newDomain) {
try {
byte[] bytes = Files.readAllBytes(path);
String str = new String(bytes);
str = str.replaceAll(oldDomain, newDomain);
Files.write(path, str.getBytes(StandardCharsets.UTF_8), StandardOpenOption.TRUNCATE_EXISTING);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}