Android开发 读取assets目录下的txt文件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/pkandroid/article/details/52451859
/**
     * 读取assets下的txt文件,返回utf-8 String
     * @param context
     * @param fileName 不包括后缀
     * @return
     */
    public static String readAssetsTxt(Context context,String fileName){
        try {
            //Return an AssetManager instance for your application's package
            InputStream is = context.getAssets().open(fileName+".txt");
            int size = is.available();
            // Read the entire asset into a local byte buffer.
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            // Convert the buffer into a string.
            String text = new String(buffer, "utf-8");
            // Finally stick the string into the text view.
           return text;
        } catch (IOException e) {
            // Should never happen!
//            throw new RuntimeException(e);
            e.printStackTrace();
        }
        return "读取错误,请检查文件名";
    }

直接上代码简单粗暴

阅读更多

没有更多推荐了,返回首页