【转】Android下使用Properties文件保存程序设置
原文:http://jerrysun.blog.51cto.com/745955/804789
废话不说,直接上代码。
读取.properties文件中的配置:
- String strValue = "";
- Properties props = new Properties();
- try {
- props.load(context.openFileInput("config.properties"));
- strValue = props.getProperty (keyName);
- System.out.println(keyName + " "+strValue);
- }
- catch (FileNotFoundException e) {
- Log.e(LOG_TAG, "config.properties Not Found Exception",e);
- }
- catch (IOException e) {
- Log.e(LOG_TAG, "config.properties IO Exception",e);
- }
相信上面这段代码大部分朋友都能看懂,所以就不做过多的解释了。
向.properties文件中写入配置:
- Properties props = new Properties();
- try {
- props.load(context.openFileInput("config.properties"));
- OutputStream out = context.openFileOutput("config.properties",Context.MODE_PRIVATE);
- Enumeration<?> e = props.propertyNames();
- if(e.hasMoreElements()){
- while (e.hasMoreElements()) {
- String s = (String) e.nextElement();
- if (!s.equals(keyName)) {
- props.setProperty(s, props.getProperty(s));
- }
- }
- }
- props.setProperty(keyName, keyValue);
- props.store(out, null);
- String value = props.getProperty(keyName);
- System.out.println(keyName + " "+value);
- }
- catch (FileNotFoundException e) {
- Log.e(LOG_TAG, "config.properties Not Found Exception",e);
- }
- catch (IOException e) {
- Log.e(LOG_TAG, "config.properties IO Exception",e);
- }
上面这段代码,跟读取的代码相比,多了一个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文件保存程序设置的更多相关文章
- Android下使用Properties文件保存程序设置
原文:http://jerrysun.blog.51cto.com/745955/804789 废话不说,直接上代码. 读取.properties文件中的配置: String strValue ...
- java读取项目根路径下和任意磁盘位置下的properties文件
1.读取项目根路径下的properties文件比较简单也是比较常见的一种操作. 具体代码如下: package com.xuanen.util; import java.util.Properties ...
- Spring下读取properties文件
由于在spring的xml文件中配置了 <bean id="validator" class="org.springframework.validation.bea ...
- cocos2d-x 2.x.x 新建工程 android下的 org文件夹丢失
cocos2d-x 2.x.x 新建工程之后... 打开android项目..会发现src下没有org文件... 解决方法一: cocos2d-x-2.2.0\cocos2dx\platform\an ...
- 在Spring环境下存取properties文件…
Spring中PropertyPlaceholderConfigurer的使用 (1) 基本的使用方法是 classpath:/spring/include/dbQuery.properties 其中 ...
- Android studio 读取properties文件
System.out.println(Thread.currentThread().getContextClassLoader().getResource("").getPath( ...
- 封装的方法--读取任何路径下的properties文件中的值
概述:我们在做项目时,经常需要从某个properties文件中读取properties文件中的值.现在我封装了一下方法,直接读取配置文件中的值. 代码如下所示: /** * Created by qi ...
- QSettings保存程序设置
今天看了一些QSettings的简单用法,可以用来保存程序的设置,使得程序每次启动都可以显示上次关闭时的状态.我这里实现了一个简单的文本编辑窗口,可以设置文本的字体,字体的颜色和背景色.每次关闭程序都 ...
- 将properties文件的配置设置为整个Web应用的全局变量。
四种作用域: Web应用中的变量存放在不同的jsp对象中,会有不一样的作用域,四种不同的作用域排序是 pageContext < request < session < applic ...
随机推荐
- SQL存储过程教程
一直以来,对SQL SERVER的存储过程和触发器都基本没有用到,只是偶尔从网上找几个简单的函数PASTE到我的SQL中用.自己写总是感觉缺点什么,前几天单位的培训讲了一天的SQL SERVER, ...
- 常用的NodeJS模块
图片处理 1.Manipulate images 官网:http://github.com/aheckmann/gm ImageMagick和GraphicsMagick主要用于图片的创建.编辑.合成 ...
- jenkins + gerrit 自动code review
最近有需求要push review以后自动跑一些测试,如果通过就自动+2 不通过就-2,目前做法如下(jenkins gerrit均已配置好,Jenkins可以连接gerrit并拉代码): 1. Je ...
- my stackoverflow
https://stackoverflow.com/questions/48017641/how-to-monitor-elastic-stack-without-x-pack https://sta ...
- What-are-P-NP-NP-complete-and-NP-hard
https://www.amazon.com/Computational-Complexity-Approach-Sanjeev-Arora/dp/0521424267 http://theory.c ...
- 如何打开chrome中flash debug player
If you’ve installed the latest version of Google Chrome, and you are having a problem debugging your ...
- Spark中的IsNotNull函数怎么用
Spark中的IsNotNull函数怎么用 在这里看到的这个函数,就是判断是否为空,但是开始不知道怎么用,后来找到了,要在View中用,也就是SparkSQL中.如下: spark.sql(" ...
- android adb 读写模式 挂载文件系统
http://www.qylk.blog.163.com/blog/static/1346873562013092154430/ http://blog.sina.com.cn/s/blog_9906 ...
- Leetcode:Interleaving String 解题报告
Interleaving StringGiven s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For ...
- repo常用指令
下载 repo 的地址: http://android.git.kernel.org/repo ,可以用 wget http://android.git.kernel.org/repo 或者 curl ...