1

I have created a Java Game and when the game finishes, a method is executed that tells the user to enter his his/her name then their score will save in playscores.txt document. This is working fine. However, i want the more than just one person's score in this document. I want it so everyone that plays the game name and score will be saved in this document. Would really appreciate some help.

This is my gameComplete Method code:

public void gameComplete() throws IOException {
    String name = (String) JOptionPane.showInputDialog(
            frame,
            "Enter your name: ",
            "Save Score",
            JOptionPane.PLAIN_MESSAGE);
    Score score = new Score(player.getScore(), name);
    FileWriter fstream = new FileWriter("playerscores.txt");
    BufferedWriter out = new BufferedWriter(fstream);
    out.write("Name : " + score.getName() + System.getProperty( "line.separator" )  );
    out.write("Score : " + score.getScore());
    out.close();
}

I have tried different stuff, such as Objectoutputstream but unfortunately cannot figure out how to do it and was wondering if it is even possible. Furthermore, i would like to know what Class i should be using to get this done.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Also [here](http://stackoverflow.com/questions/3005124/writing-to-an-already-existing-file-using-filewriter-java), and also [this question](http://stackoverflow.com/questions/1616746/java-filewriter-overwrite?rq=1). – Hovercraft Full Of Eels Apr 01 '13 at 21:45
  • @HovercraftFullOfEels We get the point i myself the creator of the post have asked for it to be deleted as there are duplicate posts. – James Danny Apr 01 '13 at 21:46

3 Answers3

1

If you're happy to just append the new score to the end of the file, replace:

FileWriter fstream = new FileWriter("playerscores.txt");

with:

FileWriter fstream = new FileWriter("playerscores.txt", true);

If you can have more than one user playing at the same time, you'll need to use file locking too, to avoid race conditions when accessing the file.

Martin Ellis
  • 9,603
  • 42
  • 53
  • Thank you Martin for that, never knew it was that easy! Unfortunately i cannot accept your answer for another 8 minutes! – James Danny Apr 01 '13 at 21:29
  • @JamesDanny: it won't matter if you accept him or not since this question will likely be closed as a duplicate. It's only been asked hundreds of times before. – Hovercraft Full Of Eels Apr 01 '13 at 21:30
1

In order to do it for more than one person, you should open the file in append mode.

FileWriter fstream = new FileWriter("playerscores.txt",true);

Syntax: public FileWriter(File file, boolean append)

Parameters:

  • file - a File object to write to

  • append - if true, then bytes will be written to the end of the file rather than the beginning.

By default, the append parameter is false. So , earlier, you were over-writing the score of the previous player with the current one.

Ritesh Kumar Gupta
  • 5,055
  • 7
  • 45
  • 71
1

Firstly, if you want the file to be added to rather than wiped and written to each time then make sure you add the second argument of true to have it append the text.

You could use a CSV file to store the answers in columns and then read them out parsing the data by using commas.

FileWriter fileW = new FileWriter("playerscores.txt", true);

Hope that helps.

Henders
  • 1,195
  • 1
  • 21
  • 27