2012-10-26 137 views
2

是否可以在活動中打開.txt文件或顯示.txt文件的內容?我真的需要它爲我的項目。在我的項目中,我製作了expandablelistview,它在父列表中顯示單元,在子列表中顯示章節。一旦選擇了孩子,選定的孩子將打開適當的.txt文件。有沒有可能這樣做。請給它一些建議。在活動中打開.txt文件

回答

0

你可以讓TextView在Activity中顯示文本,在TextView上設置文本的使用方法setText(CharSequence)方法。

來讀取文本文件,使用文本下面的方法:

FileInputStream fis=null; 
final StringBuffer buffer= new StringBuffer(); 

try { 
    fis = openFileInput("fileName.txt"); 
    DataInputStream dataIO = new DataInputStream(fis); 
    String strLine = null; 

    if ((strLine = dataIO.readLine()) != null) { 
     buffer.append(strLine); 
    } 

    dataIO.close(); 
    fis.close(); 
} 
catch (Exception e) { 
} 

使用

textView.setText(buffer.toString()); 

在活動展示。

+1

相同的代碼片段是[here](http://stackoverflow.com/questions/3344551/how-to-read-text-file-in-android) – Sajmon

0

是的。

你只需要一個File並閱讀它。只需使用Commons IO庫從apache.org

//file located directly on SD root. 
File myFile = new File(Environment.getExternalStorageDirectory(), "file.txt"); 
String contents = FileUtils.readFileToString(myFile); 

,只是使用類似myTextView.setText(contents);後設置文本到TextView

下載共享IO庫here

0

我認爲在其中顯示一個帶有textview的xml文件,並設置textview以顯示您想要顯示的任何內容都可以實現。

textview.setText(open file you want to read from here); 

打開你應該使用緩衝讀者和IO流的文件。