Android 读取res文件中raw的json文件 的工具类StreamUtils

来源:互联网 发布:java web订餐系统 编辑:程序博客网 时间:2024/05/06 00:09
public class StreamUtils {public static String get(Context context, int id) {InputStream stream = context.getResources().openRawResource(id);return read(stream);}public static String read(InputStream stream) {return read(stream, "utf-8");}public static String read(InputStream is, String encode) {        if (is != null) {            try {                BufferedReader reader = new BufferedReader(new InputStreamReader(is, encode));                StringBuilder sb = new StringBuilder();                String line = null;                while ((line = reader.readLine()) != null) {                    sb.append(line + "\n");                }                is.close();                return sb.toString();            } catch (UnsupportedEncodingException e) {                e.printStackTrace();            } catch (IOException e) {                e.printStackTrace();            }        }        return "";    }}

用法:

String data = StreamUtils.get(APP.getInstance(), R.raw.update);



0 0
原创粉丝点击