我创建了一个游戏,将您的高分保存在名为highscores.txt的文本文件中。当我打开游戏时,显示正确的高分。但是当我打开文本文件时,它总是空的。为什么是这样?这是我编写和阅读文本文件的代码。为什么我的文本文件总是空的?
FileInputStream fin = new FileInputStream("highscores.txt");
DataInputStream din = new DataInputStream(fin);
highScore = din.readInt();
highSScore.setText("High Score: " + highScore);
din.close();
FileOutputStream fos = new FileOutputStream("highscores.txt");
DataOutputStream dos = new DataOutputStream(fos);
dos.writeInt(highScore);
dos.close();
因为你在文本编辑器中打开一个包含非文本的文件? – immibis
@immibis这可能是一个很好的答案。 – Sinkingpoint
好的。 OP,如果你使用的是Unix系统,请尝试“xxd highscores.txt”,看你是否得到文本或二进制文件。 –