Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

How Do I Use A Database Research Tools You Can Use

Authors: Laura La Bella

1st Edition

1622753763, 978-1622753765

More Books

Students also viewed these Databases questions

Question

What is meant by resource-based view of the firm?

Answered: 1 week ago