在android程序中使用配置文件properties
来源:互联网 发布:2345看图王软件下载 编辑:程序博客网 时间:2024/06/15 15:06
转至:http://www.cnblogs.com/SummerRain/archive/2013/08/18/3266584.html
在android程序中使用配置文件来管理一些程序的配置信息其实非常简单
在这里我们主要就是用到Properties这个类
直接给函数给大家 这个都挺好理解的
- 读写函数分别如下:
- //读取配置文件
- public Properties loadConfig(Context context, String file) {
- Properties properties = new Properties();
- try {
- FileInputStream s = new FileInputStream(file);
- properties.load(s);
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- return properties;
- }
- //保存配置文件
- public boolean saveConfig(Context context, String file, Properties properties) {
- try {
- File fil=new File(file);
- if(!fil.exists())
- fil.createNewFile();
- FileOutputStream s = new FileOutputStream(fil);
- properties.store(s, "");
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- return true;
- }
所以它们一样可以在其他标准的java程序中被使用
在Android中,比起用纯字符串读写并自行解析,或是用xml来保存配置,
Properties显得更简单和直观,因为自行解析需要大量代码,而xml的操作又远不及Properties方便
贴一段测试的代码
- private Properties prop;
- public void TestProp(){
- boolean b=false;
- String s="";
- int i=0;
- prop=loadConfig(context,"/mnt/sdcard/config.properties");
- if(prop==null){
- //配置文件不存在的时候创建配置文件 初始化配置信息
- prop=new Properties();
- prop.put("bool", "yes");
- prop.put("string", "aaaaaaaaaaaaaaaa");
- prop.put("int", "110");//也可以添加基本类型数据 get时就需要强制转换成封装类型
- saveConfig(context,"/mnt/sdcard/config.properties",prop);
- }
- prop.put("bool", "no");//put方法可以直接修改配置信息,不会重复添加
- b=(((String)prop.get("bool")).equals("yes"))?true:false;//get出来的都是Object对象 如果是基本类型 需要用到封装类
- s=(String)prop.get("string");
- i=Integer.parseInt((String)prop.get("int"));
- saveConfig(context,"/mnt/sdcard/config.properties",prop);
- }
此时文件将被保存在 /data/data/package_name/files下,并交由系统统一管理
用此方法读写文件时,不能为文件指定具体路径
0 0
- 在android程序中使用配置文件properties
- 在android程序中使用配置文件properties
- 在android程序中使用配置文件properties
- 在android程序中使用配置文件properties
- 在Spring配置文件中使用properties文件
- java中配置文件 .properties使用
- Android Studio中使用properties配置文件出现中文乱码问题
- 在Android开发中使用properties
- 在spring中读取properties配置文件
- 在项目中获取properties等配置文件
- 在配置文件中读取:jdbc.properties文件
- Android自定义配置文件(.properties文件)的使用
- Java中封装配置文件(1)-使用Properties
- Java中封装配置文件(1)-使用Properties
- Java中封装配置文件(1)-使用Properties
- Java中封装配置文件(1)-使用Properties
- Java中封装配置文件使用Properties
- 问题:使用java中properties加载配置文件
- org.apache.ibatis.type.TypeException: Error setting null for parameter #1 with JdbcType OTHER
- Android 使用android-support-multidex解决Dex超出方法数的限制问题,让你的应用不再爆棚
- 技术普及帖:你刚才在淘宝上买了一件东西(转载)
- robotium—只有apk文件的测试
- ArrayList引起的ConcurrentModificationException 异常原因及解决方法
- 在android程序中使用配置文件properties
- Responder Chain(ios事件传递)
- 类别的作用、继承/扩展与类别的区别
- Eclipse配置Spring IDE
- 使用Memcached、Spring AOP构建数据库前端缓存框架
- IO流操作中创建的流对象为什么一定close?
- python yeild使用浅析
- 使用蒲公英来做iOS测试应用的分发 - 唐巧的技术博客
- allegro中出光绘文件遇到问题的解决办法