Android App开发记录 —配置文件的功能

来源:互联网 发布:2017年网络剧 编辑:程序博客网 时间:2024/05/21 06:31

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();
}
}