Android属性值使⽤,Android开发使⽤Properties读写属性值Properties概述
Java中的配置⽂件常为.properties⽂件,⽽Properties类便是读写此类⽂件的⼯具。属性⽂件有两种格式,⼀种是⽂本格式,其内容
是“键=值”的形式,⽂本注释信息可以⽤"#"来注释。另⼀种是XML格式,键值对遵循XML规范,Android的SharedPreferences也是以xml存储的。
下⾯是Properties的常⽤⽅法:
load : 从属性⽂件中加载属性对象
store : 把属性对象保存到属性⽂件
getProperty : 获取属性值
setProperty : 设置属性值
loadFromXML : 从XML格式的属性⽂件中加载属性对象
storeToXML : 把属性对象保存到XML格式的属性⽂件
instruction sign有哪些
Properties实际应⽤
下⾯是Properties的⼯具类代码例⼦:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
t.Context;
import android.os.Environment;
import android.util.Log;
public class PropertiesUtil {
private final static String TAG = "PropertiesUtil";
private Context mContext;
private String mPath;
private String mFile;
查看数据库表修改记录
private Properties mProp;
private static PropertiesUtil mPropUtil = null;
public static PropertiesUtil getInstance(Context context) {
if (mPropUtil == null) {
mPropUtil = new PropertiesUtil();
mPropUtil.mContext = context;
mPropUtil.mPath = ExternalStorageDirectory() + "/ExmKeyValue"; mPropUtil.mFile = "properties.ini";
}
return mPropUtil;
}
public PropertiesUtil setPath(String path) {
mPath = path;
return this;
}
public PropertiesUtil setFile(String file) {
mFile = file;
return this;
}
public PropertiesUtil init() {
指针变量是存放地址的变量Log.d(TAG, "path="+mPath+"/"+mFile);
try {
File dir = new File(mPath);
if (!ists()) {
dir.mkdirs();
}
File file = new File(dir, mFile);
if (!ists()) {
}
InputStream is = new FileInputStream(file);
mProp = new Properties();
mProp.load(is);
is.close();
} catch (Exception e) {
e.printStackTrace();
}
return this;
}
public void commit() {
try {
File file = new File(mPath + "/" + mFile);
OutputStream os = new FileOutputStream(file);
mProp.store(os, "");
os.close();
} catch (Exception e) {
e.printStackTrace();
}
mProp.clear();
}
public void clear() {
mProp.clear();
}
public void open() {
mProp.clear();
try {
File file = new File(mPath + "/" + mFile);
InputStream is = new FileInputStream(file);
mProp = new Properties();
mProp.load(is);
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void writeString(String name, String value) {
mProp.setProperty(name, value);
}
public String readString(String name, String defaultValue) { Property(name, defaultValue);
}
public void writeInt(String name, int value) {
mProp.setProperty(name, ""+value);
}
public int readInt(String name, int defaultValue) {
return Integer.Property(name, ""+defaultValue));
}
public void writeBoolean(String name, boolean value) {
mProp.setProperty(name, ""+value);
}
public boolean readBoolean(String name, boolean defaultValue) {
return Boolean.Property(name, ""+defaultValue)); }
public void writeDouble(String name, double value) {
mProp.setProperty(name, ""+value);
}
public double readDouble(String name, double defaultValue) {
return Double.Property(name, ""+defaultValue)); }
}
下⾯是向属性⽂件写⼊键值对的代码:
bigdecimal转string类型精度问题PropertiesUtil mProp = Instance(this).init();
mProp.writeString("name", "Mr Lee");
authentication failed
mProp.writeInt("age", (int)(Math.random()*100%100));
mProp.writeBoolean("married", true);
mProp.writeDouble("weight", 100f);
mProp.writeString("time", NowDateTime());
mPropmit();
properties文件用什么打开下⾯是从属性⽂件读取键值对的代码:
PropertiesUtil mProp = Instance(this).init();
mProp.open();
String name = adString("name", "");
int age = adInt("age", 0);
boolean married = adBoolean("married", false);
double weight = adDouble("weight", 0f);
String time = adString("time", "");

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。