Load a simple text file in Android Studio

后端 未结 3 845
-上瘾入骨i
-上瘾入骨i 2020-12-05 00:16

Got a brand new project using Google\'s new Android Studio IDE.

I\'m trying to load a simple text file using an InputStreamReader. I\'m getting a file n

3条回答
  •  死守一世寂寞
    2020-12-05 01:06

    This code will work for you.It will fetch all data from file.

    public class Quiz extends Activity {
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quiz);
        try {
            PlayWithRawFiles();
        } catch (IOException e) {
            Toast.makeText(getApplicationContext(),
                    "Problems: " + e.getMessage(), Toast.LENGTH_LONG).show();
        }
    }// onCreate
    
    public void PlayWithRawFiles() throws IOException {
        String str="";
        StringBuffer buf = new StringBuffer();
        InputStream is = this.getResources().openRawResource(R.raw.ashraf);
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        if (is!=null) {
            while ((str = reader.readLine()) != null) {
                buf.append(str + "\n" );
            }
        }
        is.close();
       TextView tv=(TextView)findViewById(R.id.tv1);
        tv.setText(buf.toString());
    
    
    }//
            }
    

提交回复
热议问题