Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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 Machine Performance Modeling Methodologies And Evaluation Strategies Lncs 257

Authors: Francesca Cesarini ,Silvio Salza

1st Edition

3540179429, 978-3540179429

More Books

Students also viewed these Databases questions

Question

What is management growth? What are its factors

Answered: 1 week ago