0

I'm writing a code where in data in a file has to be replaced with another file content.

I know how to use a string Replace() function. but the problem here is, I want to replace a string with a entirely new Data.

I'm able to append(in private static void writeDataofFootnotes(File temp, File fout)) the content, but unable to know how do I replace it.

Below is my code.

import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.Closeable; import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.URL; public class BottomContent { public static void main(String[] args) throws Exception { String input = "C:/Users/u0138039/Desktop/Proview/TEST/Test/src.html"; String fileName = input.substring(input.lastIndexOf("/") + 1); URL url = new URL("file:///" + input); BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); File fout = new File("C:/Users/u0138039/Desktop/TEST/Test/OP/" + fileName); File temp = new File("C:/Users/u0138039/Desktop/TEST/Test/OP/temp.txt"); if (!fout.exists()) { fout.createNewFile(); } if (!temp.exists()) { temp.createNewFile(); } FileOutputStream fos = new FileOutputStream(fout); FileOutputStream tempOs = new FileOutputStream(temp); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos)); BufferedWriter tempWriter = new BufferedWriter(new OutputStreamWriter(tempOs)); String inputLine; String footContent = null; int i = 0; while ((inputLine = in.readLine()) != null) { if (inputLine.contains("class=\"para\" id=\"")) { footContent = inputLine.replaceAll( "<p class=\"para\" id=\"(.*)_(.*)\" style=\"text-indent: (.*)%;\"><a href=\".*\">(.*)</a>(.)(.*)</p>", "<div class=\"tr_footnote\">\n<div class=\"footnote\">\n<sup><a name=\"ftn.$2\" href=\"#f$2\" class=\"tr_ftn\">$4</a></sup>\n" + "<div class=\"para\">" + "$6" + "\n</div>\n</div>\n</div>"); inputLine = inputLine.replaceAll( "<p class=\"para\" id=\"(.*)_(.*)\" style=\"text-indent: (.*)%;\"><a href=\".*\">(.*)</a>(.)(.*)</p>", ""); tempWriter.write(footContent); tempWriter.newLine(); } inputLine = inputLine.replace("</body>", "<hr/></body>"); bw.write(inputLine); bw.newLine(); } tempWriter.close(); bw.close(); in.close(); writeDataofFootnotes(temp, fout); } private static void writeDataofFootnotes(File temp, File fout) throws IOException { FileReader fr = null; FileWriter fw = null; try { fr = new FileReader(temp); fw = new FileWriter(fout, true); int c = fr.read(); while (c != -1) { fw.write(c); c = fr.read(); } } catch (IOException e) { e.printStackTrace(); } finally { close(fr); close(fw); } } public static void close(Closeable stream) { try { if (stream != null) { stream.close(); } } catch (IOException e) { // ... } } } 

Here I'm searching for a particular string and saving it in a separate txt file. And once I'm done with the job. I want to replace the <hr /> tag with the entire txt file data.

How can I achieve this?

3
  • What is working? Are your class="para" id=" lines being correctly transformed? Is your temporary file of foot notes being properly generated? Commented Mar 30, 2016 at 15:00
  • Hi @AJNeufeld, Yes everything is working fine. I just want to replace <hr /> with the entire temp.txt file contents. Commented Mar 30, 2016 at 15:05
  • 1
    Instead of substituting </body> with <hr/></body>, and then substituting <hr/> with your contents, you could skip a step and substitute </body> with <!-- footnotes --></body>. This is safer, as there could be many <hr/> lines in your document, but there is very likely only one </body>. See answer below. Commented Mar 30, 2016 at 15:31

1 Answer 1

1

I'd modify your processing loop as follows:

while ((inputLine = in.readLine()) != null) { // Stop translation when we reach end of document. if (inputLine.contains("</body>") { break; } if (inputLine.contains("class=\"para\" id=\"")) { // No changes in this block } bw.write(inputLine); bw.newLine(); } // Close temporary file tempWriter.close(); // Open temporary file, and copy verbatim to output BufferedReader temp_in = Files.newBufferedReader(temp.toPath()); String footnotes; while ((footnotes = temp_in.readLine()) != null) { bw.write(footnotes); bw.newLine(); } temp_in.close(); // Finish document bw.write(inputLine); bw.newLine(); while ((inputLine = in.readLine()) != null) { bw.write(inputLine); bw.newLine(); } // ... and close all open files 
Sign up to request clarification or add additional context in comments.

2 Comments

The above code assumes </body> appears at the start of a line. You'd need to do some additional work if that is not the case: splitting the line, substitutions (if any) and outputting first part, then outputting footnotes, then outputting remainder of line and following lines.
You are genius my friend. Working Awesomeeeeeeeeeee. Thanks a ton.:)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.