【问题标题】:how to read text file from assets in fragment?如何从片段中的资产中读取文本文件?
【发布时间】:2015-01-21 10:57:06
【问题描述】:

我应该如何从assets 读取文本文件?

我在这里使用片段。 看起来我不能对getAssets(); 使用任何东西,因为然后我得到:Error:(88, 37) error: cannot find symbol method getAssets() 目标是能够读取/写入多个字符串。

例如:

AssetManager assetManager = getAssets();
InputStream inputStream = null;
try {
    inputStream = assetManager.open("hello.txt");
}
catch (IOException e){
    Log.e("message: ",e.getMessage());
}

我希望能够在一个位置读取值,然后在代码的另一个位置写入值。请帮忙,我很绝望,我在网上找到的都没有工作

【问题讨论】:

  • 您需要一个上下文来调用该方法。您可以从片段中调用 getActivity 来获取该上下文。
  • 仍然不知道它是如何工作的

标签: android file text fragment assets


【解决方案1】:
public class Tab3 extends  Fragment {
    private TextView textView;
    private StringBuilder text = new StringBuilder();

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        String content = null;
        try {

            InputStream is = getActivity().getAssets().open("i.txt");
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            content = new String(buffer,"UTF-8");
        } catch (IOException ex) {
            ex.printStackTrace();
            Log.d("error","error");
        }


            TextView output= (TextView)getView().findViewById(R.id.editText2);
            output.setText(content);

        }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.tab_3, container, false);
         return rootView;
    }
}

这就是我在选项卡式活动中所做的。


注意把你的资产文件放在:\appname\app\src\main\assets

【讨论】:

    【解决方案2】:

    使用上面 Devendra Dora 的示例,我发现以大写字母开头的资产子目录名称不起作用。将子目录名称更改为全小写就像一个魅力。 Android Studio 3.1.2 没有抱怨我一直使用的大写命名(无济于事),但这个例子帮助我解决了我的问题。谢谢!

    【讨论】:

      【解决方案3】:
       public String loadTextFileFromAsset() {
          String content = null;
          try {
      
              InputStream is = getContext().getAssets().open("dora.txt");
              int size = is.available();
              byte[] buffer = new byte[size];
              is.read(buffer);
              is.close();
              content = new String(buffer,"UTF-8");
          } catch (IOException ex) {
              ex.printStackTrace();
              return null;
          }
          return content;
      }
      

      在 src/main/assets 文件夹中添加文本文件(比如 dora.txt)

      【讨论】:

        【解决方案4】:

        试试这个课程

        import android.content.Context;
        import android.content.res.AssetManager;
        import android.util.Log;
        import java.io.BufferedReader;
        import java.io.IOException;
        import java.io.InputStream;
        import java.io.InputStreamReader;
        public class DummyDataReader {
            Context context;
        
            public DummyDataReader(Context context) {
                this.context = context;
            }
        
            public String ReadTextFromFile(String file) {
                String data = "";
                AssetManager assetManager = context.getResources().getAssets();
                InputStream inputStream = null;
                try {
                    inputStream = assetManager.open(file);
                    StringBuilder buf = new StringBuilder();
                    BufferedReader in =
                            new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
                    String str;
                    while ((str = in.readLine()) != null) {
                        buf.append(str);
                    }
                    in.close();
                    data = buf.toString();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return data;
            }
        
        }
        

        要读取文件使用这个函数

        String response = new DummyDataReader(getActivity()).ReadTextFromFile("hello.txt");
        

        【讨论】:

          【解决方案5】:

          getAssets()Context 上的一个方法。您可以在您的Fragment 上调用getActivity(),然后在其上调用getAssets()

          【讨论】:

          • 谢谢,但是有没有合适的方法从文本文件中读取值并将它们保存在我的变量中?
          • @user2864772:我不知道你的“变量”是什么。资产在 Android 设备上并不是严格意义上的“文本文件”——您只需使用 AssetManager 上的 open() 在其内容上获得 InputStream。然后,您可以使用标准 Java 文件 I/O 使用 InputStream
          • 有多种方法可以从文件中读取值。但由于我们不知道您如何建立您的文件,我们无法提供建议..
          • 我打算写 7 个字符串值。逐行。示例如下所示:2.bp.blogspot.com/-AvGwqzCuxKc/U5vqRUglUOI/AAAAAAAADsc/…
          • @user2864772:您无法写入资产。您可以从资产中读取,但您必须在其他地方写入,例如 internal storage 上的文件。
          猜你喜欢
          • 1970-01-01
          • 2017-12-02
          • 1970-01-01
          • 1970-01-01
          • 2020-11-27
          • 2012-03-21
          • 1970-01-01
          相关资源
          最近更新 更多