0

I know how I can append text to a file so that new text will appear at the end of a text file but I want to append it so that the new text shows up in the middle instead like this.

My text file:
Beginning
Middle <--(inserted text)
End

The code below is supposed to read the text file until it finds the line that comes before where the new text is going to go and insert it there in a new line but the loop only reads nulls and without it, I'm appending to the end. Is there a way I can find the spot I need to insert the text in the text file so I can insert it there?

        File f = new File("Testing.txt");
        FileReader fr = new FileReader(f);
        FileWriter fw = new FileWriter(f, true);
        BufferedReader br = new BufferedReader(fr);
        BufferedWriter bw = new BufferedWriter(fw);

        //while(br.readLine() != "Beginning") {
            //System.out.println(br.readLine());
        //}

        bw.write("\nMiddle");
        br.close();
        fr.close();
        bw.close();
        fw.close():
  • 1
    Does this answer your question? [Best Way to Write Bytes in the Middle of a File in Java](https://stackoverflow.com/questions/181408/best-way-to-write-bytes-in-the-middle-of-a-file-in-java) – Cortex0101 Nov 24 '21 at 22:50

0 Answers0