Exercise 1: Write a program that reads a text file, removes all occurrences of a
ID: 3804403 • Letter: E
Question
Exercise 1: Write a program that reads a text file, removes all occurrences of a specified String and stores the remaining words in another text file. You may assume that the text file contains Strings, one on each line.
Exercise 0: Review the following three exercises that were discussed in the lectures. //A simple program that illustrates reading from a text file. It reads each line from the given text file and displays them on the screen import java.util. Scanner; import java.io. public class File ReadDemol public static void main (String[ args) throws IOException Scanner keyboard. Scanner System. in) new System out. print Enter the name of the file to read from String filename keyboard. nextLine File inFile new File (filename) Scanner input new Scanner (inFile); while (input.hasNext String line input. nextLine System.out.println (line) input close This program illustrates how to read from a URL /It opens the connection to the specified URL and reads each line and //displays it It also counts the total number of characters on the //webpage import java util Scanner import java.net. URL import java.io. public class ReadFileFromURL public static void main (String args) throws Exception Scanner keyboard new Scanner (System in) System. out.print Enter a URL String URLString keyboard. nextLine URL url new URL (URL string) int count Scanner input new Scanner (url openstream while input. hasNext ())f String line input. nextLine System.out.println (line); count+Jline length System. out.println ("The file size is count charactersExplanation / Answer
Here is the working solution-
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class StringReplacer {
private static void removeStringAndWriteIntoFile(String inputFileLocation,String outputFileLocation,String removeString){
FileReader fr=null;
BufferedWriter bw=null;
FileWriter fw=null;
BufferedReader br=null;
String line="";
try {
fr = new FileReader(inputFileLocation); //creating fileReader object from input file location
br = new BufferedReader(fr);
fw=new FileWriter(outputFileLocation); //creating fileWriter object from input file location
bw=new BufferedWriter(fw);
while ((line = br.readLine()) != null) {
//applying replaceAll method on string to replace all occurrence of string to empty string ""
String newLine=line.replaceAll("\b"+removeString+"\b", ""); // appended \b to too & fro near of removeString to find exact match of the string
if(!newLine.equals("")){ //if after removing the string, newLine is not empty then only we are writing it into file
bw.write(newLine);
bw.write(" "); //adding next line after adding one line into output file.
}
}
}
catch (IOException e)
{
e.printStackTrace();
}finally{
try {
fr.close();//closing the resources as it not required now.
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the name of the file to read from including path-");
String inputFileLocation=sc.next(); // path to input.txt file
System.out.println("Enter the string to remvoe:");
String removeString=sc.next();
System.out.println("Enter the name of the file to write into including path-");
String outputFileLocation=sc.next(); // path to output.txt file
removeStringAndWriteIntoFile(inputFileLocation,outputFileLocation,removeString);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.