2012-10-05 62 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

相關問題