-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReplaceText.java
More file actions
34 lines (30 loc) · 904 Bytes
/
Copy pathReplaceText.java
File metadata and controls
34 lines (30 loc) · 904 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
import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;
public class ReplaceText {
public static void main(String[] args) throws Exception{
if(args.length!=4) {
System.out.println("Usage: java ReplaceText sourceFile targetFile oldStr newStr");
System.exit(0);
}
File sourceFile=new File(args[0]);
if(!sourceFile.exists()) {
System.out.println("Source file "+args[0]+" already exist");
System.exit(0);
}
File targetFile=new File(args[1]);
if(!sourceFile.exists()) {
System.out.println("Target file "+args[1]+" already exist");
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();
}
}