在Android中读取txt文件并输出为TextView [英] Reading a txt file and outputing as a TextView in Android

查看:789
本文介绍了在Android中读取txt文件并输出为TextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试读取已保存在目录中的文本文件,并将其作为TextView打印在屏幕上.这是我到目前为止的代码.但是,当我运行该应用程序时,它会创建一个吐司,上面写着错误读取文件".我在这里做错了什么?

I am trying to read a text file that is already saved in my directory and print it on the screen as a TextView. This is the code that I have so far. However, when I run the application, it creates a toast which says "Error Reading File". What am I doing wrong here?

public class sub extends Activity {

private TextView text;


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.text);
    //text = (TextView) findViewById(R.id.summtext);
    //File file = new File("inputNews.txt");        
    //StringBuilder text = new StringBuilder();
    try {
        InputStream in = openFileInput("inputNews.txt");

        if(in != null){
            InputStreamReader reader = new InputStreamReader(in);
            BufferedReader br = new BufferedReader(reader);
            StringBuilder text = new StringBuilder();
            String line;
            while ((line = br.readLine()) != null) {
                text.append(line);
                text.append('\n');
            }   
            in.close();            

        }

    }
    catch (IOException e) {
        Toast.makeText(getApplicationContext(),"Error reading file!",Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }


    TextView output= (TextView) findViewById(R.id.summtext);
    output.setText((CharSequence) text);

}

}

推荐答案

如果要在项目中保留.txt文件,则必须在assets文件夹中找到它.
然后,您可以使用 AssetManger 进行访问.
阅读有关如何创建assets文件夹,然后使用此代码的主题:

If you want to keep a .txt file in your Project, you must locate it in the assets folder.
Then you can access it with AssetManger .
Read this topic on how to create your assets folder, and then use this code:

public class subActivity extends Activity {

private TextView textView;
private StringBuilder text = new StringBuilder();

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.text);
    BufferedReader reader = null;

    try {
        reader = new BufferedReader(
            new InputStreamReader(getAssets().open("inputNews.txt")));

        // do reading, usually loop until end of file reading  
        String mLine;
        while ((mLine = reader.readLine()) != null) {
            text.append(mLine);
            text.append('\n');
        }
    } catch (IOException e) {
        Toast.makeText(getApplicationContext(),"Error reading file!",Toast.LENGTH_LONG).show();
        e.printStackTrace();
    } finally {
        if (reader != null) {
        try {
            reader.close();
        } catch (IOException e) {
            //log the exception
        }
    }

    TextView output= (TextView) findViewById(R.id.summtext);
    output.setText((CharSequence) text);

 }
}

这篇关于在Android中读取txt文件并输出为TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆