Question
I need to take an input file ct_test.txt and change all of its line breaks to ~ except for when the term |Final|Reported| is in
I need to take an input file ct_test.txt and change all of its line breaks to ~ except for when the term |Final|Reported| is in a line. I have a code but it will not write anything to the new file. Anyone have any suggestions on where my code is going wrong?
package firstprojectfideminterop;
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.util.*;
public class FirstProjectFidemInterop { static void modifyFile(String filePath, String oldString, String newString) throws FileNotFoundException{ String oldContent = ""; File file = new File(filePath); try{ FileReader fr = new FileReader(file); FileWriter fw = new FileWriter("new_ct_test.txt"); BufferedReader br = new BufferedReader(fr); String line = br.readLine(); while(line != null){ oldContent = oldContent + line + System.lineSeparator(); line = br.readLine(); } String newContent = oldContent.replaceAll(oldString, newString); BufferedWriter bw = new BufferedWriter(fw); bw.write(newContent); } catch (IOException e){ e.printStackTrace(); } } public static void main(String[] args) throws IOException { File file = new File("ct_test.txt"); Scanner in = new Scanner(file); String keyword = "|Final|Reported|"; while(in.hasNextLine()){ String currentLine = in.nextLine(); if(currentLine.contains(keyword)){ modifyFile("ct_test.txt", " ", " "); } else{ modifyFile("ct_test.txt", " ", "~"); } System.out.println(currentLine); } System.out.println("done"); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started