2017-05-26 71 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中。 –