Possible Duplicate:
android reading from a text file
所以我需要加载文本,但我不知道如何:(为了保存文本,我正在这样做
File logFile = new File("sdcard/data/agenda.file");
if (!logFile.exists())
{
try
{
logFile.createNewFile();
}
catch (IOException e)
{
e.printStackTrace();
}
}
try
{
//BufferedWriter for performance, true to set append to file flag
BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true));
buf.append(editText1.getText());
buf.newLine();
buf.close();
}
catch (IOException e)
{
e.printStackTrace();
}
那么,如何通过点击按钮加载它呢?
要读取文件的内容,例如 *.txt - 这样做...
private String GetPhoneAddress() {
File file = new File(Environment.getExternalStorageDirectory() + "/reklama/tck.txt");
if (!file.exists()){
String line = "Need to add smth";
return line;
}
String line = null;
//Read text from file
//StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
line = br.readLine();
}
catch (IOException e) {
//You'll need to add proper error handling here
}
return line;
}
然后,从 activite 设置为 textview - 就像做 smthing 一样
final TextView tvphone = (TextView) findViewById(R.id.saved_phone);
String saved_phone = GetPhoneAddress();
if (saved_phone.length()>0){
tvphone.setText(saved_phone);
}