【问题标题】:reading Text from file in assets从资产中的文件中读取文本
【发布时间】:2014-06-04 07:05:06
【问题描述】:

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

的字符串

"项目1,项目2,项目3

我如何将文本读入数组中,以便我可以敬酒以逗号分隔的三个项目中的任何一个 读完这里的帖子后,加载文件的方式如下

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

但不知道如何进入数组

感谢您的帮助

标记

【问题讨论】:

    标签: android arrays text


    【解决方案1】:

    这是一种方式:

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

    【讨论】:

      【解决方案2】:

      这是一个示例代码:

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

      【讨论】:

        猜你喜欢
        • 2017-12-02
        • 2012-03-21
        • 1970-01-01
        • 1970-01-01
        • 2023-03-11
        • 2012-12-26
        • 2015-01-21
        相关资源
        最近更新 更多