Load a simple text file in Android Studio Load a simple text file in Android Studio android android

Load a simple text file in Android Studio


  1. Step 1: Open in Name_Project-Name_Project.iml file.
  2. See the line : option name="ASSETS_FOLDER_RELATIVE_PATH" value="/src/main/assets"
  3. Step 2: Create a sub-folder "assets" in main folder.
  4. Step 3: Put file in this folder.
  5. Step 4: Load it. Done.


The correct answer didn't work for me exactly.This works:

Go to Project view and then go to app/src/main and create new directory assets

to load the file:

   InputStream is = getApplicationContext().getAssets().open("bla.txt");

or:

   InputStream is = context.getAssets().open("bla.txt");

and then convert it to string at any way you want, examples here

detailed video of how to do it (not mine)


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

public class Quiz extends Activity {@Overridepublic 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();    }}// onCreatepublic 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());}//        }