Skip to content
Advertisement

Is there a way where I can detect text and not overwrite it?

I am trying to write this
myWriter.write(name + " has scored " + count + " hacker levels in " + duration +" milli-seconds with a delay of " + delay + " milli-seconds.")
onto my scores.txt file.
This is what I want the output in the scores.txt file to look like:
Write save here and then skip line
Write save here and then skip line (repeat again and again)

My Problem

Every time I press the save score button, it runs this code
myWriter.write(name + " has scored " + count + " hacker levels in " + duration +" milli-seconds with a delay of " + delay + " milli-seconds.")
which is good. But whenever I press the save score button again, the original line gets overwritten which I don’t want to happen.

What I’ve Tried

I have tried rn and BufferedWriter and it doesn’t match what I want the outcome to be.

My Code

HackerGUI.java

JavaScript

Main.java

JavaScript

Advertisement

Answer

If you do not want to overwrite the text each time you need to append. You can do this by initializing your FileEriter as follows:

FileWriter myWriter = new FileWriter(“scores.txt”, true);

This constructor takes 2 parameters, one is the file you are writing to and the second is a boolean expression that determines will you append to the file or overwrite it.

If you want to know more about it you can check it out here: https://www.geeksforgeeks.org/java-program-to-append-a-string-in-an-existing-file/

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement