使用场景,在assert文件夹下 有些文本文件,我们需要通过工具类读取出来,然后放到String字符串中,我们该如何操作呢:
直接上代码:
public static String readAssertResource(Context context, String strAssertFileName) { AssetManager assetManager = context.getAssets(); String strResponse = ""; try { InputStream ims = assetManager.open(strAssertFileName); strResponse = getStringFromInputStream(ims); } catch (IOException e) { e.printStackTrace(); } return strResponse; } private static String getStringFromInputStream(InputStream a_is) { BufferedReader br = null; StringBuilder sb = new StringBuilder(); String line; try { br = new BufferedReader(new InputStreamReader(a_is)); while ((line = br.readLine()) != null) { sb.append(line); } } catch (IOException e) { } finally { if (br != null) { try { br.close(); } catch (IOException e) { } } } return sb.toString(); } }
时间: 2024-11-06 03:44:51