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

android 读取properties乱码,android studio开发

2015-08-24 16:20 531 查看
  Properties 默认是按ISO-8859-1读取的。

 

       代码如下:

 
/**
* 读取assets目录下properties文件,去key对应值
* @param context
* @param fileName
* @param key
* @return
*/
public static String getAssetFilePropertyValue(Context context,String fileName,String key){
if(context ==null || fileName == null || key == null || key.equals("")) return "";
Properties p = new Properties();
InputStream in = null;
try {
AssetManager assetManager = context.getResources().getAssets();
in = assetManager.open(fileName,AssetManager.ACCESS_BUFFER);
p.load(in);
String result  = p.getProperty(key);
if(result != null && !result.equals("")){
result=new String(result.getBytes("ISO-8859-1"), "utf-8");
}
return result;
} catch (IOException e) {
e.printStackTrace();
} finally {
if(in!=null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息