您的位置:首页 > 移动开发 > Android开发

Android读程序包的资源

2015-06-05 14:59 399 查看
private static final String ENCODING = "utf-8";

public static String loadText(Context context, String assetFilePath){
InputStream is = null;
try {
is = context.getResources().getAssets().open(assetFilePath);
String textfile = convertStreamToString(is);
return textfile;
} catch (IOException e) {
e.printStackTrace();
}

return null;
}

public static String convertStreamToString(InputStream is)
throws IOException {
Writer writer = new StringWriter();

char[] buffer = new char[2048];
try {
Reader reader = new BufferedReader(new InputStreamReader(is,
ENCODING));
int n;
while ((n = reader.read(buffer)) != -1) {
writer.write(buffer, 0, n);
}
} finally {
is.close();
}
String text = writer.toString();
return text;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: