Search code examples
javaandroidfileembedded-resource

Android read in a .txt file packaged with app


I want to be able to read in a file, myFile.txt, that I ship with my app, either in the assets folder, the res/raw folder, where ever, it does not matter.

BUT! I do not want an InputStream of that file... I want to be able to do something like:

while ((reader = is.readLine()) != null) { ... }

I've searched around and found stuff that was close but not really what I'm looking for..

Thank you for any help.

EDIT - some code I was trying:

InputStream in = this.mCtx.getResources().openRawResource(R.raw.myFile);
Reader is = new BufferedReader(new InputStreamReader(in, "UTF8"));

(now what? The Reader class doesn't have a readLine() method)


Solution

  • Just change

    Reader is = new BufferedReader(new InputStreamReader(in, "UTF8"));
    

    to

    BufferedReader is = new BufferedReader(new InputStreamReader(in, "UTF8"));
    

    BufferedReader has a readLine() method. You're unnecessarily upcasting to Reader and losing the additional features.