我正在製作一個應用程序,在其中設置了我的學校平衡設置,並且可以按專用按鈕根據購買的內容取走一定的金額。我在平衡方面遇到麻煩。我有辦法將它更新爲我想要的,但是在終止應用程序之後,我希望它在TextView
中保持不變。到目前爲止,我能夠通過視頻將它保存到一個文件中,或者至少我希望是我。如何在打開應用程序時閱讀文本文件?
我怎麼能打開應用程序讀取文本文件,並設置TextView
等於文件中的內容?
submitBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String submitAmount = amountEnter.getText().toString();
balance.setText(submitAmount);
String myBalance = balance.getText().toString();
try {
FileOutputStream fou = openFileOutput("balance.txt",
MODE_WORLD_READABLE);
OutputStreamWriter osw = new OutputStreamWriter(fou);
try {
osw.write(myBalance);
osw.flush();
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
});
得到它你可以使用'SharedPreference'來存儲您的數據。這裏是官方的Android文檔:[SharedPreference](https://developer.android.com/training/basics/data-storage/shared-preferences.html) – Pulak
你的文件位於哪裏? SD卡或內存? –
將文件保存到文件中可能很危險,因爲文件可能被移動或刪除(或更糟糕)。考慮使用SharedPreferences – Thecave3