replace a string using Text i/o
This program is to replace all the occurrences of a specific string from a text file.
let say to remove John from a text file.
on i/o this topic i just started learning it and im not really sure what are my logical errors. So can somebody please give me some advice on this program. Thanks!
in command prompt i passed in
java removeText.java John john.txt
Code:
import java.io.*;
import java.util.*;
public class removeText
{
public static void main (String[] args) throws Exception
{
// Check command line parameter usage
if(args.length !=2)
{
System.out.println("Usage: java Exercise8.21 John FileName");
System.exit(0);
}
//Check FileName if exists
File SourceFile = new File(args[1]);
if(!SourceFile.exists())
{
System.out.println("Source file "+args[1]+" does not exist");
System.exit(0);
}
//Create input and output files
Scanner input= new Scanner(SourceFile);
PrintWriter output = new PrintWriter(SourceFile);
while(input.hasNext())
{
String s1 = input.nextLine();
String s2 = s1.replaceAll(args[0], "");
output.println(s2);
}
input.close();
output.close();
}//end of main
}//end of removeText