Android|Android 读取ini文件配置项

这个是带Section的文件读取,ini文件放在assets目录下
使用方法:
String titleCount = ConfigMgr.getInstance(MainActivity.this).readProperties("TestGroup", "titleCount");

ini配置:
; 测试分组 [TestGroup] ; 标题的个数 titleCount=4

代码:
【Android|Android 读取ini文件配置项】注意:STR_CONFIG_FILE 值的设置一定是你自己的ini文件名称,否则会找不到文件的
package com.yejinmo.example.utils; import android.content.Context; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.HashMap; import java.util.Properties; /** * @className: ConfigMgr * @author: yejinmo * @date: 2019/6/3 21:18 * @Description: ini文件读取工具类 */ public class ConfigMgr { /** 配置文件 */ private static final String STR_CONFIG_FILE = "study.ini"; private static HashMap sections = new HashMap<>(); private static transient Properties properties; private static ConfigMgr sConfigMgr; private final Context mContext; private ConfigMgr(Context context) { mContext = context.getApplicationContext(); }public static synchronized ConfigMgr getInstance(Context context) { if (null == sConfigMgr) { sConfigMgr = new ConfigMgr(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 = https://www.it610.com/article/line.substring(i + 1); properties.setProperty(name, value); } } } }

    推荐阅读