BufferedWriter is acting strange

后端 未结 3 1761
小蘑菇
小蘑菇 2021-01-16 14:02

I am trying to make a game with a working highscore mechanism and I am using java.io.BufferedWriter to write to a highscore file. I don\'t have an encryption on the highscor

相关标签:
3条回答
  • 2021-01-16 14:50

    Please use writer.write(String.valueOf(score)); otherwise it writes score as a character. See the documentation:

    Writes a single character. The character to be written is contained in the 16 low-order bits of the given integer value; the 16 high-order bits are ignored.

    What you want to use is Writer.write(String); convert score to a String using String.valueOf or Integer.toString.

    writer.write(String.valueOf(score));
    
    0 讨论(0)
  • 2021-01-16 14:58

    BufferedWriter is attempting to write a series of bytes to the file, not numbers. A number is still a character.

    Consider using FileWriter instead, and something as simple as: fileWriter.write(Integer.toString(score)) Write takes a string here, but the output should be the same.

    0 讨论(0)
  • 2021-01-16 15:00

    BufferedWriter.write(int) is meant to write a single charecter, not a integer.

    public void write(int c)
    throws IOException

    Writes a single character.

    Overrides: write in class Writer
    Parameters: c - int specifying a character to be written
    Throws: IOException - If an I/O error occurs

    Try

    writer.write(String.valueOf(score));  
    
    0 讨论(0)
提交回复
热议问题