小编use*_*438的帖子

读取txt文件并在Android中输出为TextView

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

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 …
Run Code Online (Sandbox Code Playgroud)

android text ioexception

1
推荐指数
1
解决办法
1万
查看次数

标签 统计

android ×1

ioexception ×1

text ×1