2017-05-26 108 views
-1

我在我的UI上有一個TextEdit,一個按鈕和一個textview。我的資產文件夾中也有一堆文本文件。所以我試圖在TextEdit中輸入一個隨機文件名,並將該(隨機命名的)文本文件加載到TextView中。我是新人,我不能說得對。我在那裏呆了幾天。它不像我所期望的那樣工作。我試圖做的方式就像Android:通過按鈕隨機命名文本文件到Textview中

(assets/TextEdit.getText(),".txt") 

該文本文件不加載。

public void Button12345(View v) 
{ 
    AssetManager assetManager = getAssets(); 
    InputStream input; 
    String text = ""; 

    try { 
     input = assetManager.open(TextEdit.getText(),".txt"); 

     int size = input.available(); 
     byte[] buffer = new byte[size]; 
     input.read(buffer); 
     input.close(); 

     // byte buffer into a string 
     text = new String(buffer); 

    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 
+0

發表您的堆棧跟蹤或告訴我們,當你執行上面的代碼會發生什麼。 – Shriram

+0

代碼不起作用或textview不加載任何文件。 –

回答

0

試試這個我的朋友會爲你工作

String test=""; 
    BufferedReader reader = null; 
    try { 
     reader = new BufferedReader(
       new InputStreamReader(getAssets().open("test.txt"), "UTF-8")); 

     // do reading, usually loop until end of file reading 
     String mLine; 
     while ((mLine = reader.readLine()) != null) { 
      //process line 

      test += mLine; 

     } 
     Toast.makeText(this, ""+test, Toast.LENGTH_SHORT).show(); 
    } catch (IOException e) { 
     //log the exception 
    } finally { 
     if (reader != null) { 
      try { 
       reader.close(); 
      } catch (IOException e) { 
       //log the exception 
      } 
     } 
    } 
+0

我只是試圖通過TextEdit將隨機文本文件加載到TextView中。 –