栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Android读ini配置文件

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

*自己的测试结构

就是这样。 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1036008.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号