Question: Java Chaptder 12 **12.11 ( Remove text ) Write a program that removes all the occurrences of a specified string from a text file. For

Java Chaptder 12 **12.11 (Remove text) Write a program that removes all the occurrences of a specified string from a text file. For example, invoking java Exercise12_11 John filename removes the string John from the specified file. Your program should get the arguments from the command line

Here is my code.

//Importing the required packages. import java.io.*; import java.util.*; public class RemoveText { // main method. public static void main(String[] args) { // Checking for invalid command line entry. if(args.length != 2) { System.out.println("Usage: java RemoveText sourceFile oldStr"); System.exit(0); } File sourceFile = new File(args[0]); // Checking the existence of the file. if(!sourceFile.exists()) { System.out.println("Source file does not exist. Program will exit. "); System.exit(0); } File tempFile = new File("temp_" + args[0]); // Creating the Scanner object. Scanner input = new Scanner(sourceFile); // Creating the PrintWriter object. PrintWriter output = new PrintWriter(tempFile); while(input.hasNext()) { String s1 = input.nextLine(); // Removing the text. String s2 = s1.replaceAll(args[1],""); output.println(s2); } input.close(); // Closing the stream. output.close(); // Closing the stream. sourceFile.delete(); // Deleting the original file. tempFile.renameTo(sourceFile); // Renaming the temp file. tempFile.delete(); // Deleting the temp file. } }

-------------------]

getting these error

RemoveText.java:25: error: unreported exception FileNotFoundException; must be caught or declared to be thrown Scanner input = new Scanner(sourceFile); ^ RemoveText.java:27: error: unreported exception FileNotFoundException; must be caught or declared to be thrown PrintWriter output = new PrintWriter(tempFile); Please help. Am I off base? 2 errors Can you please type you answer?

Thanks

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!