Android读写properties配置文件
写这篇文章之前可以成功运行,文章后就各种找不到文件.所以并没有采用此种方式,后期完善.详见下篇解决方案.
配置文件读取很容易,修改需要注意权限,比如assets目录下就不允许修改.
配置文件的创建:
New --- File
命名后选择properties方式打开
配置文件设置
contrastIP = 192.166.1.65:8011
assets目录创建
在main目录下,与java res 目录同级创建.
New --- Folder --- Assets Folder
assets目录详解: http://blog.csdn.net/chuntiandejiaobu10/article/details/52352128
权限配置
在 AndroidManifest.xml 中添加:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
其实我去掉后测试也可以成功运行.还是加上.预防万一.
先读配置:
方法一 读取assets目录下的配置
Properties props = new Properties();
props.load(context.getAssets().open(configName));
将configName文件从assets目录下放出来,放在main目录下:
方法二 会出现错误 open failed: ENOENT (No such file or directory) 然而并不知道目录路径该如何填
Properties props = new Properties();
props.load(new FileInputStream(configName));
方法三 这样就能成功运行
Properties props = new Properties();
props.load(context.openFileInput(configName));
修改配置:
Properties props = new Properties();
props.load(context.openFileInput(configPath));
props.setProperty(keyName, keyValue); // 读取assets目录下的,但是根本无法修改
// FileOutputStream out = context.getAssets().openFd(configPath).createOutputStream();
// 提示 open failed: EROFS (Read-only file system)
// FileOutputStream out = new FileOutputStream(configPath); // 这样就可以了
FileOutputStream out = context.openFileOutput(configPath,Context.MODE_PRIVATE);
参考Context.MODE_PRIVATE说明: http://www.cnblogs.com/yjpjy/p/5407251.html
完整代码
类
ProperTies 类
package com.**.demo.utils; import android.content.Context;
import android.util.Log; import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties; public class ProperTies {
//private static String configPath = getExternalStorageDirectory() + File.separator + "appConfig";
private static String configPath = "appConfig"; public static Properties getProperties(Context context) {
Log.e("configPath", configPath); Properties urlProps;
Properties props = new Properties();
try {
//方法一:通过activity中的context攻取setting.properties的FileInputStream
//注意这地方的参数appConfig在eclipse中应该是appConfig.properties才对,但在studio中不用写后缀
//InputStream in = c.getAssets().open("appConfig.properties"); //props.load(context.getAssets().open(configName)); //方法二:通过class获取setting.properties的FileInputStream
//InputStream in = PropertiesUtill.class.getResourceAsStream("/assets/ setting.properties ")); // 方法三
props.load(context.openFileInput(configPath));
// props.load(new FileInputStream(configPath)); } catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} urlProps = props;
return urlProps;
} //保存配置文件
public static String setProperties(Context context, String keyName, String keyValue) {
Properties props = new Properties();
try {
props.load(context.openFileInput(configPath));
props.setProperty(keyName, keyValue);
// FileOutputStream out = context.getAssets().openFd(configPath).createOutputStream();
FileOutputStream out = context.openFileOutput(configPath,Context.MODE_PRIVATE);
// FileOutputStream out = new FileOutputStream(configPath);
props.store(out, null); } catch (Exception e) {
e.printStackTrace();
Log.e("setPropertiesError", e.toString());
return "修改配置文件失败!";
}
return "设置成功";
} }
UrlString类:
package com.**.demo.json; import android.content.Context;
import com.**.demo.utils.ProperTies; import java.util.Properties; /**
* 读写配置属性类
*/ public class UrlString { private String contrastIPName = "contrastIP"; // 上传路径
private String ip;
private String ipAddress; public void setIPAddress(Context context) {
Properties proper = ProperTies.getProperties(context);
this.ip = proper.getProperty(contrastIPName, "");
this.ipAddress = "http://" + this.ip + "/index.html";
} public String setIPAddress(Context context, String keyValue) {
String result = ProperTies.setProperties(context, contrastIPName, keyValue);
this.ip = keyValue;
this.ipAddress = "http://" + this.ip + "/index.html";
return result;
} public String getIP() {
return this.ip;
} public String getIPAddress() {
return this.ipAddress;
}
}
在activity中使用:
加载配置文件:
private UrlString urlString = new UrlString(); editText = (EditText) findViewById(R.id.et_ip);
// 加载配置的信息 --- IP地址
urlString.setIPAddress(this);
editText.setText(urlString.getIP()); // 获取完整地址
// urlString.getIPAddress()
修改配置文件:
String value = editText.getText().toString();
String result = urlString.setIPAddress(this,value); tools.customToast(result, ConfigActivity.this);
Android读写properties配置文件的更多相关文章
- java 顺序 读写 Properties 配置文件
java 顺序 读写 Properties 配置文件 支持中文 不乱码 java 顺序 读写 Properties 配置文件 ,java默认提供的Properties API 继承hashmap ,不 ...
- 【转】Java 读写Properties配置文件
[转]Java 读写Properties配置文件 1.Properties类与Properties配置文件 Properties类继承自Hashtable类并且实现了Map接口,也是使用一种键值对的形 ...
- Java 读写Properties配置文件
Java 读写Properties配置文件 JAVA操作properties文件 1.Properties类与Properties配置文件 Properties类继承自Hashtable类并且实现了M ...
- java 顺序 读写 Properties 配置文件 支持中文 不乱码
java 顺序 读写 Properties 配置文件 ,java默认提供的Properties API 继承hashmap ,不是顺序读写的. 特从网上查资料,顺序读写的代码,如下, import j ...
- (转)Java 读写Properties配置文件
原文:http://www.cnblogs.com/xudong-bupt/p/3758136.html 1.Properties类与Properties配置文件 Properties类继承自Hash ...
- java读写properties配置文件方法
1.Properties类 Properties类表示了一个持久的属性集.Properties可保存在流中或从流中加载,属性列表中的key和value必须是字符串. 虽然Properties类继承了j ...
- Java 读写Properties配置文件【转】
1.Properties类与Properties配置文件 Properties类继承自Hashtable类并且实现了Map接口,也是使用一种键值对的形式来保存属性集.不过Properties有特殊的地 ...
- Properties类读写.properties配置文件
package com.hzk.utils; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFo ...
- java读写properties配置文件不改变属性的顺序和注释
先贴代码 import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java ...
随机推荐
- Could not find package vendor/name in a version matching v-Number 是坑!
当我遇到这个问题的时候曾去发布了issue -https://github.com/composer/packagist/issues/934 主要的问题是,composer require vend ...
- GateOne Web SSH 环境搭建
环境配置安装python及tornadoyum -y install python-pippip install tornado GateOne安装下载源码:git clone https://git ...
- LOJ #2533. 「CTSC2018」暴力写挂(边分治合并)
题意 给你两个有 \(n\) 个点的树 \(T, T'\) ,求一对点对 \((x, y)\) 使得 \[ depth(x) + depth(y) - (depth(LCA(x , y)) + dep ...
- [THUWC2017]随机二分图
题目大意 给一张二分图,有左部点和右部点. 有三种边,第一种是直接从左部点连向右部点,出现概率为50%. 第二种边一组里有两条边,这两条边同时出现或者不出现,概率都是50%. 第三种边一组里有两条边, ...
- 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位dp)
题目链接:https://ac.nowcoder.com/acm/contest/163/J 题目大意:给定一个数N,求区间[1,N]中满足可以整除它各个数位之和的数的个数.(1 ≤ N ≤ 1012 ...
- 为 Java 程序员准备的 Go 入门 PPT
为 Java 程序员准备的 Go 入门 PPT 这是 Google 的 Go 团队技术主管经理 Sameer Ajmani 分享的 PPT,为 Java 程序员快速入门 Go 而准备的. 视频 这个 ...
- Ubuntu16.04 g++5.4依旧不支持C++11问题
jacket@jacket:~$ g++ -v Using built-in specs. COLLECT_GCC=g++ COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_6 ...
- 网页换肤,模块换肤,jQuery的Cookie插件使用(转)
具体效果如下: 第一次加载如下图: 然后点击天蓝色按钮换成天蓝色皮肤如下图: 然后关闭网页重新打开或者在打开另一个网页如下图: 因为皮肤用Cookie保存了下来,所以不会重置 具体的实现代码如下: & ...
- django系列5:视图
在Django中,网页和其他内容由视图提供.每个视图都由一个简单的Python函数(或基于类的视图的方法)表示.Django将通过检查所请求的URL(确切地说,是域名后面的URL部分)来选择视图. 在 ...
- (转)如何阅读OpenStack源码
1 关于该项目 本项目使用在线绘图工具web sequencediagrams完成,目标是图形化OpenStack的所有操作流程,通过操作序列图能快速学习Openstack的工作原理,理清各个组件的关 ...