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

By | March 12, 2011

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();
         //"air" is the name of the folder...
	folder_array= mngr_spinner.list("air");
catch (IOException e1)

“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;
	is = getAssets().open(air/Buttons Events/filename);
	int siz = is.available();
	byte[] buffer = new byte[siz];;
        //This text contains the content of the file..
	String text = new String(buffer);
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?

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

Leave a Reply to dbv Cancel reply

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