android工程里,配置文件的实现。

来源:互联网 发布:linux网站配置文件 编辑:程序博客网 时间:2024/04/30 11:44

开发程序,有时需要有配置文件,如数据库连接,及时通信服务器连接等

android工程实现该功能也非常简单,实现如下:

1、创建配置文件,如property.properties,文件内容如下:

host=10.10.27.111
hostName=a27-c-name

2、把配置文件property.properties放到工程的资产(assets)目录下.

3、在程序里读取配置文件:

   //先打开配置文件,通过项目的资产管理器(AssetManager)打开资产文件夹下的配置文件
   InputStream inputStream = mContext.getAssets().open("property.properties");

  //把配置文件的输入流加载到属性类对象里,方便读取属性

   Properties properties = new Properties();
   properties.load(inputStream);

   //读取属性

  String host = (String) properties.getProperty("host");
 String hostName = (String) properties.getProperty("hostName");

0 0
原创粉丝点击