我想在 TextView 中显示从文件中添加的所有数字的总和,目前它只是读取/显示文件中的最后一个数字。
这是我当前写入文件的代码:
total.setText(total.getText());
try {
FileOutputStream fos = openFileOutput("TotalSavings", Context.MODE_PRIVATE);
fos.write(total.getText().toString().getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
这是我当前从文件中读取的代码:
public void savingstotalbutton(View view) {
try {
BufferedReader inputReader = new BufferedReader(new InputStreamReader(
openFileInput("TotalSavings")));
String inputString;
StringBuffer stringBuffer = new StringBuffer();
while ((inputString = inputReader.readLine()) != null) {
stringBuffer.append(inputString + "\n");
}
savingstotaltext.setText(stringBuffer.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
谁能告诉我该怎么做?