forked from txs72/JavaTutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReplaceText.java
More file actions
executable file
·47 lines (45 loc) · 1.5 KB
/
ReplaceText.java
File metadata and controls
executable file
·47 lines (45 loc) · 1.5 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/* */ package ch12_io;
/* */
/* */ import java.io.File;
/* */ import java.io.PrintWriter;
/* */ import java.util.Scanner;
/* */
/* */ public class ReplaceText {
/* */ public static void main(String[] args) throws Exception {
/* 9 */ if (args.length != 4) {
/* 10 */ System.out.println("Usage: java ReplaceText sourceFile targetFile oldStr newStr");
/* */
/* 12 */ System.exit(1);
/* */ }
/* */
/* */
/* 16 */ File sourceFile = new File(args[0]);
/* 17 */ if (!sourceFile.exists()) {
/* 18 */ System.out.println("Source file " + args[0] + " does not exist");
/* 19 */ System.exit(2);
/* */ }
/* */
/* */
/* 23 */ File targetFile = new File(args[1]);
/* 24 */ if (targetFile.exists()) {
/* 25 */ System.out.println("Target file " + args[1] + " already exists");
/* 26 */ System.exit(3);
/* */ }
/* */
/* */
/* */
/* 31 */ try(Scanner input = new Scanner(sourceFile);
/* 32 */ PrintWriter output = new PrintWriter(targetFile)) {
/* */
/* 34 */ while (input.hasNext()) {
/* 35 */ String s1 = input.nextLine();
/* 36 */ String s2 = s1.replaceAll(args[2], args[3]);
/* 37 */ output.println(s2);
/* */ }
/* */ }
/* */ }
/* */ }
/* Location: /Volumes/TXS.128G/hope useful/practice/2020.jar!/ch12_io/ReplaceText.class
* Java compiler version: 8 (52.0)
* JD-Core Version: 1.1.3
*/