为什么我的代码没有写入文件?
Why is my code not writing to file?
我试图在 activity 启动时从文本文件中读取一个整数。
在我的日志中,我只看到
调试:尝试读取高分文件
永远不会
调试:读取高分文件
或
调试:无法读取高分文件
为什么我的文件是由 writeHighScore 文件创建的,但没有写入任何值。
public class GameScreen extends AppCompatActivity implements View.OnClickListener {
private Simon simon = new Simon();;
private int score = 0;
private int highScore = 0;
private String highScoreString;
private String FILENAME = "highscore.txt";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_screen);
//Read highScore file
TextView highScoreTV = (TextView) findViewById(R.id.high_score_tv);
Log.i("DEBUG", "Attempt to read High Score File");
try{
FileInputStream fis = openFileInput(FILENAME);
Scanner scanner = new Scanner(fis);
if(scanner.hasNext()){
Log.i("DEBUG", "Reading High Score File");
highScoreString = scanner.nextLine();
highScore = Integer.getInteger(highScoreString);
}
scanner.close();
} catch (FileNotFoundException e){
Log.e("READ ERROR", "Could not read high score: " + e.getMessage());
}
highScoreTV.setText(""+highScore);
}
writeHighScore
private void writeHighScore(){
try {
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
OutputStreamWriter osw = new OutputStreamWriter(fos);
BufferedWriter bw = new BufferedWriter(osw);
PrintWriter pw = new PrintWriter(bw);
Log.i("DEBUG", "Writing High Score File");
pw.println("" + score + "\n");
} catch (FileNotFoundException e) {
Log.e("WRITE ERROR", "Cannot Save: " + e.getMessage());
e.printStackTrace();
Toast.makeText(GameScreen.this, "Error Saving", Toast.LENGTH_SHORT).show();
}
}
我认为问题是您忘记关闭打印器,因此 reader 无法打开文件。
只需向您的 writeHighScore
方法添加结束语句即可解决问题。
我试图在 activity 启动时从文本文件中读取一个整数。 在我的日志中,我只看到
调试:尝试读取高分文件
永远不会
调试:读取高分文件
或
调试:无法读取高分文件
为什么我的文件是由 writeHighScore 文件创建的,但没有写入任何值。
public class GameScreen extends AppCompatActivity implements View.OnClickListener {
private Simon simon = new Simon();;
private int score = 0;
private int highScore = 0;
private String highScoreString;
private String FILENAME = "highscore.txt";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_screen);
//Read highScore file
TextView highScoreTV = (TextView) findViewById(R.id.high_score_tv);
Log.i("DEBUG", "Attempt to read High Score File");
try{
FileInputStream fis = openFileInput(FILENAME);
Scanner scanner = new Scanner(fis);
if(scanner.hasNext()){
Log.i("DEBUG", "Reading High Score File");
highScoreString = scanner.nextLine();
highScore = Integer.getInteger(highScoreString);
}
scanner.close();
} catch (FileNotFoundException e){
Log.e("READ ERROR", "Could not read high score: " + e.getMessage());
}
highScoreTV.setText(""+highScore);
}
writeHighScore
private void writeHighScore(){
try {
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
OutputStreamWriter osw = new OutputStreamWriter(fos);
BufferedWriter bw = new BufferedWriter(osw);
PrintWriter pw = new PrintWriter(bw);
Log.i("DEBUG", "Writing High Score File");
pw.println("" + score + "\n");
} catch (FileNotFoundException e) {
Log.e("WRITE ERROR", "Cannot Save: " + e.getMessage());
e.printStackTrace();
Toast.makeText(GameScreen.this, "Error Saving", Toast.LENGTH_SHORT).show();
}
}
我认为问题是您忘记关闭打印器,因此 reader 无法打开文件。
只需向您的 writeHighScore
方法添加结束语句即可解决问题。