/**
* 读取assets中的文件
*/
private JSONArray readFromAssets() {
try {
InputStream is = getAssets().open("drama.json");//此处为要加载的json文件名称
String text = readFile(is);
JSONArray jsonArray = new JSONArray(text);
return jsonArray;
} catch (Exception e) {
// TODO Auto-generated catch block
Log.d("readFromAssets", e.toString());
return null;
}
}
/**
* 将文件中的json读取成一个字符串;
*
* @param is
* @return
*/
private String readFile(InputStream is) throws IOException {
InputStreamReader reader = new InputStreamReader(is, "utf-8");
BufferedReader bufferedReader = new BufferedReader(reader);
StringBuffer buffer = new StringBuffer("");
String str;
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
buffer.append("\n");
}
return buffer.toString();//把读取的数据返回
}
网友评论