How to read a folder in the assets directory and read files from it in android?

By | June 4, 2012

File reading is important in android. For the put the files in the assets folder.

We can able to read

  • foldername
  • filename
  • Contents of the file

First we read the name of the folder.

private String folder_array[];
AssetManager mngr_spinner = getAssets();
try
{
         //"air" is the name of the folder...
	folder_array= mngr_spinner.list("air");
}
catch (IOException e1)
{
	e1.printStackTrace();
}

“folder_array” contains all the names of the folder
Then if the path is set to

folder_array= mngr_spinner.list("air/Buttons Events");

Then the list of file name is got.
If you want to read the file content then do the following..

InputStream is;
try
{
	is = getAssets().open(air/Buttons Events/filename);
	int siz = is.available();
	byte[] buffer = new byte[siz];
	is.read(buffer);
        //This text contains the content of the file..
	String text = new String(buffer);
        is.close();
}
catch (Exception e)
{
	 Toast.makeText(CheatSheet.this,"File Not Found Error.Please ensure that file is not deleted.", Toast.LENGTH_SHORT).show();
}

2 thoughts on “How to read a folder in the assets directory and read files from it in android?

  1. dbv

    Hi Guys. Great site.

    Android question: Say person has an eReader app (eg Kindle or others). Is it possible for another app to know what book titles have been downloaded or streamed?

    Reply
  2. Pingback: Saving TextFile to SDCARD in android? | Coderz Heaven

Leave a Reply

Your email address will not be published. Required fields are marked *