forked from Joyounger/Introduction-to-Java-Programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReplaceText.java
More file actions
39 lines (32 loc) · 928 Bytes
/
ReplaceText.java
File metadata and controls
39 lines (32 loc) · 928 Bytes
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
// date:17.4.3
// author: linyang <linyang@xiaomi.com>
import java.io.*;
import java.util.*;
import static java.lang.System.out;
public class ReplaceText {
public static void main(String[] args) throws Exception {
if (args.length != 4) {
out.println("usage: java ReplaceText sourceFile targetFile oldStr newStr");
System.exit(0);
}
File sourceFile = new File(args[0]);
if (!sourceFile.exists()) {
out.println("sourceFile " + args[0] + " does not exists");
System.exit(0);
}
File targetFile = new File(args[1]);
if (targetFile.exists()) {
out.println("targetFile " + args[1] + " already exists");
System.exit(0);
}
Scanner input = new Scanner(sourceFile);
PrintWriter output = new PrintWriter(targetFile);
while (input.hasNext()) {
String s1 = input.nextLine();
String s2 = s1.replaceAll(args[2], args[3]);
output.println(s2);
}
input.close();
output.close();
}
}