Android读ini配置文件

介绍

; 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文件是这样的机构,所以随便写了一下具体的调试一下就可以了

*自己的测试结构

就是这样。 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值