1. 程式人生 > >Android開發筆記(八十四)使用Properties讀寫屬性值

Android開發筆記(八十四)使用Properties讀寫屬性值

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開發筆記的完整目錄