介绍
; initest
[initest]
key1=1
key2=2
1. ; initest , 表示该行是注释,符号后面表示注释的内容
2. [initest] 像这种 [ ]里面的就是session
3.左边的是key,右边的是value
只用到了一个读功能
public class ConfigIniSetting {
/** 配置文件 */
private static final String STR_CONFIG_FILE = "nemu.ini";
private static HashMap<String, Properties> sections = new HashMap<>();
private static transient Properties properties;
private static ConfigIniSetting sConfigMgr;
private final Context mContext;
private ConfigIniSetting(Context context) {
mContext = context;
}
public static synchronized ConfigIniSetting getInstance(Context context) {
if (null == sConfigMgr) {
sConfigMgr = new ConfigIniSetting(context);
}
return sConfigMgr;
}
public String readProperties(String section, String key) {
try {
InputStream inputStream = mContext.getResources().getAssets().open(STR_CONFIG_FILE);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
read(reader);
reader.close();
} catch (Exception ex) {
ex.printStackTrace();
}
Properties p = sections.get(section);
if (p == null) {
return null;
}
return p.getProperty(key);
}
private void read(BufferedReader reader) throws IOException {
String line;
while ((line = reader.readLine()) != null) {
parseLine(line);
}
}
private void parseLine(String line) {
line = line.trim();
if (line.matches("\\[.*\\]")) {
String section = line.replaceFirst("\\[(.*)\\]", "$1");
properties = new Properties();
sections.put(section, properties);
} else if (line.matches(".*=.*")) {
if (properties != null) {
int i = line.indexOf('=');
String name = line.substring(0, i);
String value = line.substring(i + 1);
properties.setProperty(name, value);
}
}
}
}
Main调用
binding.tvDashboardIconTestIni.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int x = 0;
StringBuffer stringBuffer = new StringBuffer();
for (int i = 0; i < ++x; i++) {
String str = ConfigIniSetting.getInstance(requireActivity()).readProperties("General","key"+x);
if (!TextUtils.isEmpty(str)){
stringBuffer.append(str+",");
}else {
String seesulter = stringBuffer.toString();
Snackbar.make(v,seesulter,Snackbar.LENGTH_LONG).setAction("点我", new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(requireActivity(),seesulter, Toast.LENGTH_SHORT).show();
}
}).show();
return;
}
}
}
});
因为测试的ini文件是这样的机构,所以随便写了一下具体的调试一下就可以了
*自己的测试结构
就是这样。