2012-01-01 39 views
1

我使用下面的代碼從我的SD卡上讀取txt文件,但是如何將它顯示爲文本視圖?如何讀取.txt文件並將其顯示爲Android中的TextView?

try{ 

       File f = new File(Environment.getExternalStorageDirectory()+"/filename.txt"); 
       FileInputStream fileIS = new FileInputStream(f); 
       BufferedReader buf = new BufferedReader(new InputStreamReader(fileIS)); 
       String readString = new String(); 
       //just reading each line and pass it on the debugger 
       while((readString = buf.readLine())!= null){ 
        Log.d("line: ", readString); 

       } 

      } catch (FileNotFoundException e) { 

       e.printStackTrace(); 

      } catch (IOException e){ 

       e.printStackTrace(); 

      } 

回答

1

這應做到:

public void displayOutput() 
{ 
    File sdcard = Environment.getExternalStorageDirectory(); 
    File file = new File(sdcard,"/TextFile.txt"); 
    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'); 
     } 
    } 
    catch (IOException e) { 
     Toast.makeText(getApplicationContext(),"Error reading file!",Toast.LENGTH_LONG).show(); 
     e.printStackTrace(); 
    } 
    catch (FileNotFoundException e) { 
     Toast.makeText(getApplicationContext(),"File not found!",Toast.LENGTH_LONG).show(); 
     e.printStackTrace(); 
    } 
    TextView output=(TextView) findViewById(R.id.output); 
    // Assuming that 'output' is the id of your TextView 
    output.setText(text); 
} 
0
LinearLayout myVerticalLinearLayout = (LinearLayout) findViewById(R.id.myLinearLayout); 
TextView text = new TextView(getApplicationContext()); 
text.setText(readString); 
myVerticalLinearLayout.addView(text); 

這應該在你應該有之前創建一個線性佈局添加垂直文本的意見。

0
public String readText(){ 
      //this is your text 
      StringBuilder text = new StringBuilder(); 
     try{ 

      File f = new File(Environment.getExternalStorageDirectory()+"/filename.txt"); 
      FileInputStream fileIS = new FileInputStream(f); 
      BufferedReader buf = new BufferedReader(new InputStreamReader(fileIS)); 

      String readString = ""; 
      //just reading each line and pass it on the debugger 
      while((readString = buf.readLine())!= null){ 
       Log.d("line: ", readString); 
       text.append(readString + "\n"); 

      } 

     } catch (FileNotFoundException e) { 

      e.printStackTrace(); 

     } catch (IOException e){ 

      e.printStackTrace(); 

     } 
      return text.toString(); 
    } 

    ... 

     TextView tv=findViewById(.....); 
     tv.setText(readText()); 
+0

它給我在線路錯誤時(返回text.toString();)?? – Leon 2012-01-01 20:57:03

+0

它仍然是一樣的? – Leon 2012-01-01 21:02:28

+0

我檢查了它,但它仍然給我一個錯誤(返回text.toString();) – Leon 2012-01-01 21:07:27

0

公共類ReadFileActivity延伸活動{

/**當第一次創建活動時調用。 */

TextView text; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    text=(TextView)findViewById(R.id.textview); 
    text.setText(readTxt()); 
} 
private String readTxt(){ 

    InputStream inputStream = getResources().openRawResource(R.raw.aaa); 

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 

    int i; 
try { 
    i = inputStream.read(); 
    while (i != -1) 
    { 
     byteArrayOutputStream.write(i); 
     i = inputStream.read(); 
    } 
    inputStream.close(); 
} catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 

    return byteArrayOutputStream.toString(); 
    } 

}

相關問題