Android App開發記錄 —配置文件的功能

1. 配置文件的功能

使用Android提供的Properties類可以很方便的實現

(1)  Properties的配置

mProperty.setProperty("IP", strIP);
mProperty.setProperty("Port", strPort);
mIP.setText("");
mPort.setText("");

(2)Properties讀取

public Properties loadConfig(Context context, String strPath) 
{
Properties properties = new Properties();
try 
{
FileInputStream s = new FileInputStream(strPath);
properties.load(s);

catch (Exception e) 
{
properties.put("IP", "192.108.0.1");
properties.put("Port", "8086");

String strDirPath = Environment.getExternalStorageDirectory() + "/WZApp";
String strFilePath = Environment.getExternalStorageDirectory() + "/WZApp/Config.txt";

if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
createDir(strDirPath);
createFile(strFilePath);
}
else if(Environment.getExternalStorageState().equals(Environment.MEDIA_REMOVED))
{
Toast.makeText(this, "沒有sdCard", 1000).show();
}

e.printStackTrace();
}

return properties;
}

(2)Properties保存

public void saveConfig(Context context, String strPath, Properties properties) 
{
try 
{
FileOutputStream s = new FileOutputStream(strPath, false);
properties.store(s, "");

catch (Exception e)
{
File file = new File(strPath);  
try
{
FileOutputStream s = new FileOutputStream(strPath, false);
properties.store(s, "");
}
catch(Exception ex)
{
ex.printStackTrace();
}


e.printStackTrace();
}
}



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