William Rose
William Rose

Reputation: 47

How to write to the next blank line in a document?

I have a basic Java web browser, and each time I visit a new site, a record of that is stored in an ArrayList. What my program currently does is write each element on a new line after each session is closed, but when a new session is started then closed, it overwrites the text created by the last session.

What I want to know is how to get the BufferedWriter to look for the next blank line in the document and start writing from there, so no text is ever overwritten and a persistent web history is kept?

Here is the code for the write method I have:

//This method writes the current sessions' history to a text document, so a persistent history is kept

private void WriteHistory() throws Exception {

    FileWriter writer = new FileWriter(outPath);
    BufferedWriter textWriter = new BufferedWriter(writer);
    for(int i = 0; i < URLBar.history.size(); i++) {
      textWriter.write(URLBar.history.get(i));
      textWriter.newLine();
    }
    textWriter.close();

}

Upvotes: 0

Views: 2841

Answers (2)

notanormie
notanormie

Reputation: 455

Easiest way is to use FileWriter(File file, boolean append) constructor.

FileWriter writer = new FileWriter(outPath, true);

This will just add text to whatever text it is already in the file.

Upvotes: 1

Wasim Wani
Wasim Wani

Reputation: 555

Before textWriter.close(); u can create a new Line by textWriter.write("\n");

and before u overwrite check for new Line

Upvotes: 0

Related Questions