Properties概述
Java中的配置文件常为.properties文件,而Properties类便是读写此类文件的工具。属性文件有两种格式,一种是文本格式,其内容是“键=值”的形式,文本注释信息可以用"#"来注释。另一种是XML格式,键值对遵循XML规范,Android的SharedPreferences也是以xml存储的。
下面是Properties的常用方法:
load : 从属性文件中加载属性对象
store : 把属性对象保存到属性文件
getProperty : 获取属性值
setProperty : 设置属性值
loadFromXML : 从XML格式的属性文件中加载属性对象
storeToXML : 把属性对象保存到XML格式的属性文件
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;
import android.content.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 = Environment.getExternalStorageDirectory() + "/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 (!dir.exists()) {
dir.mkdirs();
}
File file = new File(dir, mFile);
if (!file.exists()) {
file.createNewFile();
}
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) {
return mProp.getProperty(name, defaultValue);
}
public void writeInt(String name, int value) {
mProp.setProperty(name, ""+value);
}
public int readInt(String name, int defaultValue) {
return Integer.parseInt(mProp.getProperty(name, ""+defaultValue));
}
public void writeBoolean(String name, boolean value) {
mProp.setProperty(name, ""+value);
}
public boolean readBoolean(String name, boolean defaultValue) {
return Boolean.parseBoolean(mProp.getProperty(name, ""+defaultValue));
}
public void writeDouble(String name, double value) {
mProp.setProperty(name, ""+value);
}
public double readDouble(String name, double defaultValue) {
return Double.parseDouble(mProp.getProperty(name, ""+defaultValue));
}
}
复制
下面是向属性文件写入键值对的代码:
PropertiesUtil mProp = PropertiesUtil.getInstance(this).init();
mProp.writeString("name", "Mr Lee");
mProp.writeInt("age", (int)(Math.random()*100%100));
mProp.writeBoolean("married", true);
mProp.writeDouble("weight", 100f);
mProp.writeString("time", Utils.getNowDateTime());
mProp.commit();
复制
下面是从属性文件读取键值对的代码:
PropertiesUtil mProp = PropertiesUtil.getInstance(this).init();
mProp.open();
String name = mProp.readString("name", "");
int age = mProp.readInt("age", 0);
boolean married = mProp.readBoolean("married", false);
double weight = mProp.readDouble("weight", 0f);
String time = mProp.readString("time", "");
复制
点击下载本文用到的属性键值对存取的工程代码
点此查看Android开发笔记的完整目录