read file from assets

前端 未结 18 2252
终归单人心
终归单人心 2020-11-21 22:47
public class Utils {
    public static List getMessages() {
        //File file = new File(\"file:///android_asset/helloworld.txt\");
        AssetMan         


        
18条回答
  •  暖寄归人
    2020-11-21 23:36

    It maybe too late but for the sake of others who look for the peachy answers :

    public static String loadAssetFile(Context context, String fileName) {
        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(context.getAssets().open(fileName)));
            StringBuilder out= new StringBuilder();
            String eachline = bufferedReader.readLine();
            while (eachline != null) {
                out.append(eachline);
                eachline = bufferedReader.readLine();
            }
            return out.toString();
        } catch (IOException e) {
            Log.e("Load Asset File",e.toString());
        }
        return null;
    }
    

提交回复
热议问题