发布于 2017-03-25 21:53:36 | 343 次阅读 | 评论: 0 | 来源: 网友投递
Android移动端操作系统
Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。
本文实例讲述了Android读取本地json文件的方法。分享给大家供大家参考,具体如下:
1、读取本地JSON ,但是显示汉字乱码
public static String readLocalJson(Context context, String fileName){
String jsonString="";
String resultString="";
try {
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(
context.getResources().getAssets().open(fileName)));
while ((jsonString=bufferedReader.readLine())!=null) {
resultString+=jsonString;
}
} catch (Exception e) {
// TODO: handle exception
}
return resultString;
}
2、读取本地JSON,显示汉字正确,txt文件设置时UTF-8,UNIX
public static String readLocalJson(Context context, String fileName){
String jsonString="";
String resultString="";
try {
InputStream inputStream=context.getResources().getAssets().open(fileName);
byte[] buffer=new byte[inputStream.available()];
inputStream.read(buffer);
resultString=new String(buffer,"GB2312");
} catch (Exception e) {
// TODO: handle exception
}
return resultString;
}
希望本文所述对大家Android程序设计有所帮助。