1

i have an text file in my assets folder called test.txt. It contains a string in the form

"item1,item2,item3

How do i read the text into and array so that I can then toast any one of the three items that are deliminated by a comma After reading post here the way to load the file is as follows

AssetManager assetManager = getAssets();
InputStream ims = assetManager.open("test.txt");

But cant work out how to get into an array

your help is appreciated

Mark

user3422687
  • 229
  • 1
  • 8
  • 27

2 Answers2

0

This is one way:

         InputStreat inputStream = getAssets().open("test.txt");
         ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

         int i;
      try {
       i = inputStream.read();
       while (i != -1)
          {
           byteArrayOutputStream.write(i);
           i = inputStream.read();
          }
          inputStream.close();
      } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }

    String[] myArray = TextUtils.split(byteArrayOutputStream.toString(), ",");
Abs
  • 3,902
  • 1
  • 31
  • 30
0

Here is a sample code :

private void readFromAsset() throws UnsupportedEncodingException {       


    AssetManager assetManager = getAssets();
    InputStream is = null;

                try {
                    is = assetManager.open("your_path/your_text.txt");
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                BufferedReader reader = null;
                reader = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));

                String line = "";

                try {

                    while ((line = reader.readLine()) != null) {

                       //Read line by line here
                    }
                } catch (IOException e) {

                    e.printStackTrace();
                }



    }
Tugrul
  • 1,760
  • 4
  • 24
  • 39