首页 > 移动平台 > 详细

Android - 读取Properties配置文件

时间:2015-02-09 00:33:13      阅读:312      评论:0      收藏:0      [点我收藏+]

  在Android中读取配置文件,可以使用System.getProperties()方法读取:

    1,在res资源目录下,新建一个文件夹 raw,然后在其下创建一个.properties文件.如:

request_char=utf-8
URL=http://192.168.1.101:8080/ServerAQI/JsonAction
range_long=7
#days
from_date_name=fromDate
to_date_name=toDate

     2,可以定义一个工具类,接受android.content.res.Resources类型的参数,返回Properties对象,如:

package spt.assist;

import java.io.IOException;
import java.util.Properties;

import android.content.res.Resources.NotFoundException;
import android.util.Log;

import spt.aqi.activity.R;

public class PropertyConfig {
	/**获取配置文件信息中的指定值.
	 * @param resources
	 * @param key
	 * @return
	 */
	public static String getProperty(android.content.res.Resources resources,
			String key) {
		Properties properties = getProperties(resources);
		return properties.getProperty(key);
	}

	/**获取配置文件中的信息.
	 * @param resources
	 * @return
	 */
	public static Properties getProperties(
			android.content.res.Resources resources) {
		Properties props = new Properties();
		try {
			props.load(resources.openRawResource(R.raw.properties));
		} catch (NotFoundException e) {
			Log.i("sysout",
					"ResourceSearcher:OpenFileFromUtil:" + e.getMessage());
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			Log.i("sysout",
					"ResourceSearcher:OpenFileFromUtil:" + e.getMessage());
			e.printStackTrace();
			return null;
		}
		return props;
	}
}

     3,在Android中的资源类ContextWrapper的子类(如Activity或Service)类中调用调用getResources()方法并传入上面的工具类的方法,如,在Service类中,

final String url = PropertyConfig.getProperty(getResources(), "URL");

 

Android - 读取Properties配置文件

原文:http://www.cnblogs.com/listened/p/4280653.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!