Android读写properties配置⽂件写这篇⽂章之前可以成功运⾏,⽂章后就各种不到⽂件.所以并没有采⽤此种⽅式,后期完善.详见下篇解决⽅案.
配置⽂件读取很容易,修改需要注意权限,⽐如assets⽬录下就不允许修改.
配置⽂件的创建:
New --- File
命名后选择properties⽅式打开
配置⽂件设置
contrastIP = 192.166.1.65:8011
assets⽬录创建
在main⽬录下,与java res ⽬录同级创建.
New --- Folder --- Assets Folder
权限配置
在 l 中添加:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
其实我去掉后测试也可以成功运⾏.还是加上.预防万⼀.
先读配置:
⽅法⼀读取assets⽬录下的配置
Properties props = new Properties();
props.Assets().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 = Assets().openFd(configPath).createOutputStream();
// 提⽰ open failed: EROFS (Read-only file system)properties文件用什么打开
/
/ FileOutputStream out = new FileOutputStream(configPath);
// 这样就可以了
execute读音FileOutputStream out = context.openFileOutput(configPath,Context.MODE_PRIVATE);
完整代码
类
ProperTies 类
package com.lemon.demo.utils;
t.Context;
couchdb性能import android.util.Log;nexus手机
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";
</span><span >public</span> <span >static</span><span > Properties getProperties(Context context) {
Log.e(</span>"configPath"<span >, configPath);
Properties urlProps;
Properties props </span>= <span >new</span><span > Properties();
</span><span >try</span><span > {
</span><span >//</span><span >⽅法⼀:通过activity
中的context攻取setting.properties的FileInputStream
</span><span >//</span><span >注意这地⽅的参数appConfig在eclipse中应该是appConfig.properties才对,但在studio中不⽤写后缀
</span><span >//</span><span >InputStream in = c.getAssets().open("appConfig.properties");
</span><span >//</span><span >props.Assets().open(configName));
</span><span >//</span><span >⽅法⼆:通过class获取setting.properties的FileInputStream
</span><span >//</span><span >InputStream in = ResourceAsStream("/assets/ setting.properties "));
</span><span >//</span><span > ⽅法三</span>
props.load(context.openFileInput(configPath));
// props.load(new FileInputStream(configPath));
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
urlProps </span>=<span > props;
</span><span >return</span><span > urlProps;
}
</span><span >//</span><span >保存配置⽂件</span>
<span >public</span> <span >static</span><span > String setProperties(Context context, String keyName, String keyValue) { Properties props </span>= <span >new</span><span >
Properties();
</span><span >try</span><span > {
props.load(context.openFileInput(configPath));
props.setProperty(keyName, keyValue);
</span><span >//</span><span > FileOutputStream out = Assets().openFd(configPath).createOutputStream();</span>
FileOutputStream out =<span > context.openFileOutput(configPath,Context.MODE_PRIVATE);
</span><span >//</span><span > FileOutputStream out = new FileOutputStream(configPath);</span>
props.store(out, <span >null</span><span >);
} </span><span >catch</span><span > (Exception e) {
e.printStackTrace();
Log.e(</span>"setPropertiesError"<span >, e.toString());
</span><span >return</span> "修改配置⽂件失败!"<span >;
}
</span><span >return</span> "设置成功"<span >;
}
}
UrlString类:
package com.lemon.demo.json;
t.Context;
import com.lemon.demo.utils.ProperTies;
import java.util.Properties;
/**
读写配置属性类
*/
public class UrlString {
<span >private</span> String contrastIPName = "contrastIP"<span >;
</span><span >//</span><span > 上传路径</span>
<span >private</span><span > String ip;
</span><span >private</span><span > String ipAddress;
mybatis中oracle分页</span><span >public</span> <span >void</span><span
> setIPAddress(Context context) {
getresources多个源Properties proper </span>=<span > Properties(context);
</span><span >this</span>.ip = Property(contrastIPName, ""<span >);
</span><span >this</span>.ipAddress = "" + <span >this</span>.ip + "/index.html"<span >; }
</span><span >public</span><span > String setIPAddress(Context context, String keyValue) {
String result </span>=<span > ProperTies.setProperties(context, contrastIPName, keyValue);
</span><span >this</span>.ip =<span > keyValue;
</span><span >this</span>.ipAddress = "" + <span >this</span>.ip + "/index.html"<span >; </span><span >return</span><span > result;
}
</span><span >public</span><span > String getIP() {
</span><span >return</span> <span >this</span><span >.ip;
}
</span><span >public</span><span > String getIPAddress() {
</span><span >return</span> <span >this</span><span >.ipAddress;
}
}
在activity中使⽤:
加载配置⽂件:
private UrlString urlString = new UrlString();
editText = (EditText) findViewById(_ip);
// 加载配置的信息 --- IP地址
urlString.setIPAddress(this);
editText.IP());
// 获取完整地址
// IPAddress()
修改配置⽂件:
String value = Text().toString();
String result = urlString.setIPAddress(this,value);
tools.customToast(result, ConfigActivity.this);
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论