BufferedWriter is acting strange

余生颓废 提交于 2019-12-01 13:04: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));  

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));

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!