2017-04-08 46 views
0
public class intro extends AppCompatActivity { 

    private StringBuilder text = new StringBuilder(); 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.intro); 
     BufferedReader reader = null; 

     try { 
      reader = new BufferedReader(
        new InputStreamReader(getAssets().open("assets/Introduction.txt"))); 

      // do reading, usually loop until end of file reading 
      String mLine; 
      while ((mLine = reader.readLine()) != null) { 
       text.append(mLine); 
       text.append('\n'); 
      } 
     } catch (IOException e) { 
      Toast.makeText(getApplicationContext(),"Error reading file!", Toast.LENGTH_LONG).show(); 
      e.printStackTrace(); 
     } finally { 
      if (reader != null) { 
       try { 
        reader.close(); 
       } catch (IOException e) { 
        //log the exception 
       } 
      } 

      TextView output= (TextView) findViewById(R.id.intro); 
      output.setText((CharSequence) text); 

     } 
    } 
[This is my project explorer][1]} 

我嘗試了很多打開資產文件夾的路徑,但它顯示filenotfound異常。在這裏,我在Assets文件夾中創建了Introduction.txt文件,並在其上編寫了一些內容。但我無法通過提供路徑或文件名來打開它。我從資產文件夾中讀取文本文件時遇到了FileNotFound異常

回答

1

"assets/Introduction.txt"刪除assets/。提供給open()的路徑是內的相對路徑您的assets/

+0

我刪除但即使它不工作......它說FileNotFound – Chitrapandi

+0

@Chitrapandi:那麼你沒有'Introduction.txt'在你的資產的根目錄(例如'src/main/assets /'內你的模塊)。 – CommonsWare

+0

不,我在這裏看到我的項目瀏覽器---> https://i.stack.imgur.com/OklGw.png – Chitrapandi

相關問題