Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

hi guys, i just start lernig exceptionc in java and it is still kinda confusing. this program was given in the book. right not i

hi guys, i just start lernig exceptionc in java and it is still kinda confusing. this program was given in the book. right not i am just trying to revies program so that exceptions are handled in the main method by using the catch clause so need help with that.

this is java program

Thanks a lot

* From Chapter 12 in Liang Y Daniel, Introduction to Java Programming, 11st Edition
*/
/**
* the ReplaceText class demonstrates reading text from a file using Scanner
* and writing text to a text file using PrintWriter
*/
import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;
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);
}
// Check if source file exists
File sourceFile = new File(args[0]);
if (!sourceFile.exists()) {
System.out.println("Source file " + args[0] + " does not exist");
System.exit(2);
}
// 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);
}
try ( // try-with-resource to autoclose resources
// Create input and output files
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);
}
}
}
}

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

Database And Expert Systems Applications 24th International Conference Dexa 2013 Prague Czech Republic August 2013 Proceedings Part 1 Lncs 8055

Authors: Hendrik Decker ,Lenka Lhotska ,Sebastian Link ,Josef Basl ,A Min Tjoa

2013 Edition

3642402844, 978-3642402845

More Books

Students also viewed these Databases questions