Android讀寫配置文件

android 讀寫配置文件

基本上我們所有的數據均放在 /data/data/com.xxx.xxx目錄下:

shared_prefs/ 放置簡單的配置信息文件,文件格式爲xxx.xml;


databases/ 放置數據庫文件,文件名自己定義。


//SharedPreferences 文件讀寫

//讀文件
SharedPreferences settings = this.getContext().getSharedPreferences(
				this.getClass().getName(), 0);
		string str = settings.getString("password", "");



//寫文件
SharedPreferences settings = this.getContext().getSharedPreferences(
				this.getClass().getName(), 0);
		Editor editor = settings.edit();
		editor.putString("password", password);
		editor.commit();
		
//這裏的 this.getClass().getName()是獲得類名稱。
//getSharedPreferences(String name, int mode);
//private static String strFile = "/data/data/com.entesi.wells/files/wells.properties";

//讀文件
Properties properties = new Properties();
try {
	File fil=new File(strFile);
	if(!fil.exists())
	{
		fil.createNewFile();
		return false;
	}
	else
	{
		FileInputStream s = new FileInputStream(strFile);
		properties.load(s);
	}
} catch (Exception e) 
{
	e.printStackTrace();
	return false;
}

if ((String)properties.get("waittime")!=null)
	this.nWaitTime = Integer.parseInt((String)properties.get("waittime"));

//寫文件
try {  
		FileOutputStream s = new FileOutputStream(strFile, false);  
		Properties properties = new Properties();
		
		File fil=new File(strFile);
		if(!fil.exists()){
			fil.createNewFile();
		}
		properties.setProperty("waittime", nWaitTime+"");
		
		properties.store(s, null); 
	} catch (Exception e){  
		e.printStackTrace();  
	} 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章