簡體   English   中英

如何從片段中讀取文本文件?

[英]how to read text file from assets in fragment?

我應該如何從assets讀取文本文件?

我在這里使用片段。 看起來我不能與getAssets();一起使用任何東西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());
}

我希望能夠在一個位置讀取值,然后在代碼的另一位置寫入值。 請幫助,我很拼命,網上找不到任何東西

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

 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)

試試這個課程

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

使用上面的Devendra Dora的示例,我發現以大寫字母開頭的資產子目錄名稱不起作用。 將子目錄名稱更改為所有小寫字母就像是一種魅力。 Android Studio 3.1.2並未抱怨我一直在使用大寫命名(無濟於事),但是此示例幫助我解決了我的問題。 謝謝!

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM