从资产中的文件中读取文本

时间:2014-06-04 07:05:07

标签: android arrays text

我的资产文件夹中有一个名为test.txt的文本文件。它包含一个

形式的字符串

" ITEM1,ITEM2,项目3

我如何阅读文本和数组,以便我可以烘烤由逗号分隔的三个项目中的任何一个 在这里阅读后,加载文件的方式如下

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

但是无法弄清楚如何进入阵列

感谢您的帮助

标记

2 个答案:

答案 0 :(得分:0)

这是一种方式:

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

答案 1 :(得分:0)

以下是示例代码:

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



    }
相关问题