android - 如何从文件加载文本到textview

标签 android file-io textview

<分区>

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);
    }

关于android - 如何从文件加载文本到textview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12664226/

相关文章:

c - 使用绝对路径打开文件

c# - 如何在没有 CopyFile 或 CopyFileEx 的情况下在 Windows 上复制大文件?

c - 使用 fgets 将行复制到字符串数组中?

java - 出现空指针异常

android - Android TextView 中的内联超链接

android - 通过json在textview中显示数据

android - 使用后退按钮恢复 Activity

android - Gridview 仅显示两个项目 android

java - Android 中的 slider 开关

android - Kotlin 协同程序 - 如果一段时间后第一个任务没有完成,则开始另一个任务