美文网首页
Android 读取ini文件配置项

Android 读取ini文件配置项

作者: 夜瑾漠 | 来源:发表于2019-06-03 21:43 被阅读0次

这个是带Section的文件读取,ini文件放在assets目录下

使用方法:

String titleCount = ConfigMgr.getInstance(MainActivity.this).readProperties("TestGroup", "titleCount");

ini配置:

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

代码:

注意: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<String, Properties> 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 = line.substring(i + 1);
                properties.setProperty(name, value);
            }
        }
    }
}

相关文章

  • Android 读取ini文件配置项

    这个是带Section的文件读取,ini文件放在assets目录下 使用方法: ini配置: 代码: 注意:STR...

  • Python读取ini配置文件

    配置文件ini的格式 读取ini文件 获取配置文件中对应值的基础方法 实现一个ini配置文件读取工具类

  • Python读取ini配置文件

    配置文件ini的格式 读取ini文件 获取配置文件中对应值的基础方法 实现一个ini配置文件读取工具类

  • PHP.INI

    PHP.INI php.ini用来设置PHP的配置项。配置文件(php.ini)在 PHP 启动时被读取。对于服务...

  • Bat中读取ini文件

    很多时候配置文件会用到ini文件,在bat中可以写一个readini函数来读取ini 例如ini文件 读取测试 测试结果

  • python配置文件-conf&ini

    一、conf配置文件 1.介绍 conf配置文件和ini配置文件读取方式一致,以下以ini为后缀的配置文件为例 配...

  • Android读取ini文件

    直接上代码说明,例如 mReader = new IniReader("/data/app/xxx.ini"); ...

  • Configparser模块的使用

    ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似...

  • python configparser模块

    ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似...

  • configparser处理配置文件

    configparser 配置文件:以.conf .ini .cfg结尾的格式 a.cfg 配置文件的内容 读取配...

网友评论

      本文标题:Android 读取ini文件配置项

      本文链接:https://www.haomeiwen.com/subject/sfhhxctx.html