2012-10-05 25 views
1

无法理解如何计算行数。这是我的代码。如何计算文件txt中的行android

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('\n'); 

      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中的setText?谢谢!

+0

因此,您正在读取行,在while循环中递增计数器有什么问题? – njzk2

+0

另外,TextView在while循环中做了什么? – njzk2

+0

是的,这是我的错误 – yota9

回答

2

这是你在找什么?

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('\n'); 

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

不,我需要在textview中编写lineCount。我写te.setText(lineCount);它不会在显示屏上写任何东西 – yota9

+0

好吧我改变它显示lineCount'te.setText(String.valueOf(lineCount));' –

+0

是的!!!!!!!这是我需要的,谢谢!^_ ^ – yota9

相关问题