Yohann T.
Yohann T.

Reputation: 1925

Load a simple text file in Android Studio

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 not found exception. Now there isn't any assets/ folder. I tried to create one and add my file at many different spots (at the root of the project, at the root of the .java file, etc...) I've tried to move the file around but still get the file not found.

Now that never was a problem using Eclipse as there is an assets folder created by any template.

Does anyone know where should the assets go to or how to load them?

Here is the code used, it fails at .open():

InputStream iS = resources.getAssets().open("bla.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(iS));

I also tried this code in Eclipse, it works and the file contents get loaded. So there's probably a step needed in Android Studio.

Upvotes: 30

Views: 76540

Answers (4)

user19531522
user19531522

Reputation: 11

//Compiled all the codes available in the internet, and this works perfectly fine for reading data from a textfile //Compiled By: JimHanarose

 ArrayList<String> data_base = new ArrayList<String>();
    String text = "";
    try {
        InputStream is = getApplicationContext().getAssets().open("g.txt"); //save this .txt under src/main/assets/g.txt
        int size = is.available();
        StringBuffer buf = new StringBuffer();
        byte [] buffer = new byte[size];
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        if (is!=null) {
            while ((text = reader.readLine()) != null) {
                data_base.add(text.toString()); //create your own arraylist variable to hold each line being read from g.txt 
            }
        }
        is.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

Upvotes: 1

nogmos
nogmos

Reputation: 909

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)

Upvotes: 4

Ashraf
Ashraf

Reputation: 11

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());


}//
        }

Upvotes: 1

FIT226557
FIT226557

Reputation: 1023

  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.

Upvotes: 88

Related Questions