原文:http://jerrysun.blog.51cto.com/745955/804789

废话不说,直接上代码。
    读取.properties文件中的配置: 

  1. String strValue = "";
  2. Properties props = new Properties();
  3. try {
  4. props.load(context.openFileInput("config.properties"));
  5. strValue = props.getProperty (keyName);
  6. System.out.println(keyName + " "+strValue);
  7. }
  8. catch (FileNotFoundException e) {
  9. Log.e(LOG_TAG, "config.properties Not Found Exception",e);
  10. }
  11. catch (IOException e) {
  12. Log.e(LOG_TAG, "config.properties IO Exception",e);
  13. }

相信上面这段代码大部分朋友都能看懂,所以就不做过多的解释了。

向.properties文件中写入配置:

  1. Properties props = new Properties();
  2. try {
  3. props.load(context.openFileInput("config.properties"));
  4. OutputStream out = context.openFileOutput("config.properties",Context.MODE_PRIVATE);
  5. Enumeration<?> e = props.propertyNames();
  6. if(e.hasMoreElements()){
  7. while (e.hasMoreElements()) {
  8. String s = (String) e.nextElement();
  9. if (!s.equals(keyName)) {
  10. props.setProperty(s, props.getProperty(s));
  11. }
  12. }
  13. }
  14. props.setProperty(keyName, keyValue);
  15. props.store(out, null); 
  16. String value = props.getProperty(keyName);
  17. System.out.println(keyName + " "+value);
  18. }
  19. catch (FileNotFoundException e) {
  20. Log.e(LOG_TAG, "config.properties Not Found Exception",e);
  21. }
  22. catch (IOException e) {
  23. Log.e(LOG_TAG, "config.properties IO Exception",e);
  24. }

上面这段代码,跟读取的代码相比,多了一个if判断以及一个while循环。主要是因为Context.Mode造成的。因为我的工程涉及到多个配置信息。所以只能是先将所有的配置信息读取出来,然后在写入配置文件中。
    Context.Mode的含义如下:
    1.MODE_PRIVATE:为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容。
    2.MODE_APPEND:代表该文件是私有数据,只能被应用本身访问,该模式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件。
    3.MODE_WORLD_READABLE:表示当前文件可以被其他应用读取。
    4.MODE_WORLD_WRITEABLE:表示当前文件可以被其他应用写入。

注:.properties文件放置的路径为/data/data/packagename/files

【转】Android下使用Properties文件保存程序设置的更多相关文章

  1. Android下使用Properties文件保存程序设置

    原文:http://jerrysun.blog.51cto.com/745955/804789 废话不说,直接上代码.    读取.properties文件中的配置: String strValue ...

  2. java读取项目根路径下和任意磁盘位置下的properties文件

    1.读取项目根路径下的properties文件比较简单也是比较常见的一种操作. 具体代码如下: package com.xuanen.util; import java.util.Properties ...

  3. Spring下读取properties文件

    由于在spring的xml文件中配置了 <bean id="validator" class="org.springframework.validation.bea ...

  4. cocos2d-x 2.x.x 新建工程 android下的 org文件夹丢失

    cocos2d-x 2.x.x 新建工程之后... 打开android项目..会发现src下没有org文件... 解决方法一: cocos2d-x-2.2.0\cocos2dx\platform\an ...

  5. 在Spring环境下存取properties文件…

    Spring中PropertyPlaceholderConfigurer的使用 (1) 基本的使用方法是 classpath:/spring/include/dbQuery.properties 其中 ...

  6. Android studio 读取properties文件

    System.out.println(Thread.currentThread().getContextClassLoader().getResource("").getPath( ...

  7. 封装的方法--读取任何路径下的properties文件中的值

    概述:我们在做项目时,经常需要从某个properties文件中读取properties文件中的值.现在我封装了一下方法,直接读取配置文件中的值. 代码如下所示: /** * Created by qi ...

  8. QSettings保存程序设置

    今天看了一些QSettings的简单用法,可以用来保存程序的设置,使得程序每次启动都可以显示上次关闭时的状态.我这里实现了一个简单的文本编辑窗口,可以设置文本的字体,字体的颜色和背景色.每次关闭程序都 ...

  9. 将properties文件的配置设置为整个Web应用的全局变量。

    四种作用域: Web应用中的变量存放在不同的jsp对象中,会有不一样的作用域,四种不同的作用域排序是 pageContext < request < session < applic ...

随机推荐

  1. SQL存储过程教程

      一直以来,对SQL SERVER的存储过程和触发器都基本没有用到,只是偶尔从网上找几个简单的函数PASTE到我的SQL中用.自己写总是感觉缺点什么,前几天单位的培训讲了一天的SQL SERVER, ...

  2. 常用的NodeJS模块

    图片处理 1.Manipulate images 官网:http://github.com/aheckmann/gm ImageMagick和GraphicsMagick主要用于图片的创建.编辑.合成 ...

  3. jenkins + gerrit 自动code review

    最近有需求要push review以后自动跑一些测试,如果通过就自动+2 不通过就-2,目前做法如下(jenkins gerrit均已配置好,Jenkins可以连接gerrit并拉代码): 1. Je ...

  4. my stackoverflow

    https://stackoverflow.com/questions/48017641/how-to-monitor-elastic-stack-without-x-pack https://sta ...

  5. What-are-P-NP-NP-complete-and-NP-hard

    https://www.amazon.com/Computational-Complexity-Approach-Sanjeev-Arora/dp/0521424267 http://theory.c ...

  6. 如何打开chrome中flash debug player

    If you’ve installed the latest version of Google Chrome, and you are having a problem debugging your ...

  7. Spark中的IsNotNull函数怎么用

    Spark中的IsNotNull函数怎么用 在这里看到的这个函数,就是判断是否为空,但是开始不知道怎么用,后来找到了,要在View中用,也就是SparkSQL中.如下: spark.sql(" ...

  8. android adb 读写模式 挂载文件系统

    http://www.qylk.blog.163.com/blog/static/1346873562013092154430/ http://blog.sina.com.cn/s/blog_9906 ...

  9. Leetcode:Interleaving String 解题报告

    Interleaving StringGiven s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For ...

  10. repo常用指令

    下载 repo 的地址: http://android.git.kernel.org/repo ,可以用 wget http://android.git.kernel.org/repo 或者 curl ...