如何计算文件txt中的行数android [英] How to count lines in file txt android

查看:39
本文介绍了如何计算文件txt中的行数android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法理解如何计算行数.这是我的代码.

Can't understand how to count lines. Here is my code.

 void load() throws IOException        
{        
    File sdcard = Environment.getExternalStorageDirectory();
    File file = new File(sdcard,"agenda.file");
    StringBuilder text = new StringBuilder();

    try {
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;

        while ((line = br.readLine()) != null) {
            text.append(line);
            text.append('
');

           TextView te=(TextView)findViewById(R.id.textView1);

        }


    }
    catch (IOException e) {
        //You'll need to add proper error handling here
    }
    Button monpopb = (Button) findViewById(R.id.button13);
    monpopb.setText(text);  

}那么,如何在TextView中进行计数和设置文本呢?谢谢!

} So, how to count and settext in TextView? Thank you!

推荐答案

这是您要找的吗?

void load() throws IOException        
{        
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard,"agenda.file");
StringBuilder text = new StringBuilder();

try {
    BufferedReader br = new BufferedReader(new FileReader(file));
    String line;

    TextView te=(TextView)findViewById(R.id.textView1);
    int lineCount = 0;
    while ((line = br.readLine()) != null) {
        text.append(line);
        text.append('
');

        lineCount++;
    }
    te.setText(String.valueOf(lineCount));

}
catch (IOException e) {
    //You'll need to add proper error handling here
}
    Button monpopb = (Button) findViewById(R.id.button13);
    monpopb.setText(text);  
}

这篇关于如何计算文件txt中的行数android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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