我創建了一個遊戲,將您的高分保存在名爲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」,看你是否得到文本或二進制文件。 –