直接上代码说明,例如
mReader = new IniReader("/data/app/xxx.ini");
wifiSSID = mReader.getValueStr(SECTION, "ssid");
IniReader类:
public class IniReader implements Serializable {
/** 配置文件 */
private HashMapiniItem = new HashMap();
/** 配置文件中对应的模块名,如[Comunication] */
private transient String mSectionName;
/** 每个模块中所对应的属性 */
private transient Properties mProperties; // properties配置文件
public IniReader(String filename) throws IOException {
FileReader fReader = new FileReader(filename);
BufferedReader reader = new BufferedReader(fReader);
read(reader);
reader.close();
fReader.close();
Log.e("iniReader", "iniReader class");
}
protected void read(BufferedReader reader) throws IOException {
String line;
while ((line = reader.readLine()) != null) {
parseLine(line);
}
}
/**
* 解析从配置文件中读取到的每一行
*
* @param propertiLine
* 从配置文件中读取到的属性值
*/
private void parseLine(String propertiLine) {
propertiLine = propertiLine.trim();
Log.e("iniReader", "iniReader parseLine1:"+propertiLine+"teshu:");
/** 配对模块名 Eg:[Version]*/
//正则表达式受*.ini文件影响?
//有时候propertiLine.matches("\\[.*\\]")可行,有时候 propertiLine.matches("\\[.*?\\]")可行
//有此bug,就暂时这样使用,后续改进
if (/*propertiLine.matches("\\[.*\\]")*/propertiLine.contains("[fact_down]")) {
//mSectionName = propertiLine.replaceFirst("\\[(.*)\\]", "$1");
mSectionName = "fact_down";
mProperties = new Properties();
iniItem.put(mSectionName, mProperties);
Log.e("iniReader", "iniReader parseLine2 mSectionName:"+mSectionName);
} else if (propertiLine.matches(".*=.*")) {
/** 配对属性值 */
Log.e("iniReader", "iniReader parseLine3");
if (mProperties != null)
{
int i = propertiLine.indexOf('=');
/** 取得属性名称 */
String name = propertiLine.substring(0, i).toLowerCase();
/** 取得属性值 */
String value = propertiLine.substring(i + 1);
mProperties.setProperty(name, value);
System.out.println("name:"+name+" value"+value);
}
}
}
/**
*
* @param section
* 配置文件中模块名
* @param name
* 对应模块中的属性名称
* @return true表示有对应的测试项,false表示没有对应的测试项
*/
public boolean getValue(String section, String name) {
boolean ret = false;
Properties p = (Properties) iniItem.get(section);
/**如果没有设置该默认为显示*/
if (p == null) {
return false;
}
String value = p.getProperty(name);
ret = ("1".equals(value)) ? true : false;
return ret;
}
public String getValueStr(String section, String name) {
//boolean ret = false;
Properties p = (Properties) iniItem.get(section);
/**如果没有设置该默认为显示*/
if (p == null) {
return null;
}
String value = p.getProperty(name);
return value;
}
}