Question: Listing 12.16, ReplaceText.java, gives a program that replaces text in a source file and saves the change into a new file. Revise the program to
Listing 12.16, ReplaceText.java, gives a program that replaces text in a source file and saves the change into a new file. Revise the program to save the change into the original file. For example, invoking java Exercise12_16 file oldString newString replaces oldString in the source file with newString.
Listing

![public static void main(String[] args) throws Exception { // Check command line](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2022/11/636a72dea9dc2_606636a72de99a66.jpg)
1 import java.jo.*: 2 import java.util.*; 3 4 public class ReplaceText { public static void main(String[] args) throws Exception { // Check command line parameter usage if (args.length != 4) { System.out.println( "Usage: java ReplaceText sourceFile targetFile oldStr newStr"); System.exit(1); 10 11 12 // Check if source file exists File sourceFile = new File(args[0]); if (!sourceFile.exists()) { System.out.println("Source file " + args[0] System.exit(2); 13 14 15 16 17 18 19 20 21 22 23 24 25 26 does not exist"); // Check if target file exists File targetFile = new File(args[1]); if (targetFile.exists()) { System.out.println("Target file " + args[1] + " already exists"); System.exit(3); 27 try ( // Create input and output files Scanner input = new Scanner(sourceFile); PrintWriter output 28 29 30 new PrintWriter(targetFile); %3D 31 while (input.hasNext ()) { String sl = input.nextline(); String s2 = sl.replaceAll(args [2], args[3]); output.println(s2); 32 33 34 35 36 37 38 39 } omm mmmmm
Step by Step Solution
3.36 Rating (159 Votes )
There are 3 Steps involved in it
For executiong the program a sample file needs to be kept in the same directory where the java file ... View full answer
Get step-by-step solutions from verified subject matter experts
