读取raw中.properties文件

本文介绍了一个用于从Java资源文件中加载属性并进行编码转换的方法。该方法通过使用Properties类从指定的资源ID加载配置信息,并将读取到的属性值从ISO-8859-1编码转换为UTF-8编码,以解决可能出现的乱码问题。

直接上代码

//key为键值
public static String getJsonStr(String key, Context context) {
		Properties props = loadProperties(context);
		String apiKey = props.getProperty(key, "");
		try {
			return new String(apiKey.getBytes("ISO-8859-1"), "utf-8");//有可能有乱码,加此代码
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return "";
	}


	public static Properties loadProperties(Context context) {


		Properties props = new Properties();
		try {
			int id = context.getResources().getIdentifier("alljsonstr", "raw",
					context.getPackageName());//意思是读取raw中的alljsonstr文件
			props.load(context.getResources().openRawResource(id));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return props;
	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值