操作ini配置文件Java工具类

import java.io.*;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * GH
 * 新增配置项
 * 修改配置项
 * 删除配置项
 * 查询配置信息
 *
 */
public class IniFileUtils {

    private Map<String, Map<String, String>> sections = new LinkedHashMap<>();

    public IniFileUtils(String filePath) {
        load(filePath);
    }

    private void load(String filePath) {
        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String line;
            String currentSection = null;

            while ((line = reader.readLine()) != null) {
                line = line.trim();

                // 忽略空行和注释
                if (line.isEmpty() || line.startsWith(";") || line.startsWith("#")) {
                    continue;
                }

                // 处理节名 [section]
                if (line.startsWith("[") && line.endsWith("]")) {
                    currentSection = line.substring(1, line.length() - 1).trim();
                    continue;
                }

                // 处理键值对
                int separatorIndex = line.indexOf("=");
                if (separatorIndex == -1) {
                    continue;
                }

                String key = line.substring(0, separatorIndex).trim();
                String value = line.substring(separatorIndex + 1).trim();

                // 将键值对添加到当前节中
                if (currentSection != null) {
                    Map<String, String> section = sections.computeIfAbsent(currentSection, k -> new HashMap<>());
                    section.put(key, value);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取配置项具体值
     * @param section
     * @param key
     * @return
     */
    public String getValue(String section, String key) {
        Map<String, String> sectionMap = sections.get(section);
        if (sectionMap != null) {
            return sectionMap.get(key);
        }
        return null;
    }

    /**
     * 新增配置项
     * @param section
     * @param key
     * @param value
     */
    public void setValue(String section, String key, String value) {
        Map<String, String> sectionMap = sections.computeIfAbsent(section, k -> new HashMap<>());
        sectionMap.put(key, value);
    }

    /**
     * 删除配置项
     * @param section
     */
    public void deleteValue(String section) {
        Map<String, String> sectionMap = sections.computeIfAbsent(section, k -> new HashMap<>());
//        sectionMap.remove(section);
        sections.remove(section);

    }

    public void save(String filePath) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
            for (Map.Entry<String, Map<String, String>> sectionEntry : sections.entrySet()) {
                String section = sectionEntry.getKey();
                writer.write("[" + section + "]");
                writer.newLine();

                Map<String, String> sectionMap = sectionEntry.getValue();
                for (Map.Entry<String, String> entry : sectionMap.entrySet()) {
                    String key = entry.getKey();
                    String value = entry.getValue();
                    writer.write(key + "=" + value);
                    writer.newLine();
                }

                writer.newLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        IniFileUtils iniFileUtils = new IniFileUtils("C:\\Users\\dem0\\Desktop\\config.ini");

        // 读取配置文件
//        String value1 = iniFileUtils.getValue("Demo1", "a");
//        System.out.println("Value1: " + value1);
//
//        String value2 = iniFileUtils.getValue("Demo2", "a");
//        String value3 = iniFileUtils.getValue("Demo2", "b");
//        System.out.println("Value2: " + value2);
//        System.out.println("Value3: " + value3);


        // 设置配置文件(新增和修改
        iniFileUtils.setValue("Demo1", "a", "1101");
        iniFileUtils.setValue("Demo1", "b", "111");
        iniFileUtils.setValue("Demo1", "c", "112");
        iniFileUtils.setValue("Demo2", "d", "220");
        iniFileUtils.setValue("Demo3", "e", "1234");
//        删除配置项
        iniFileUtils.deleteValue("Demo3");
        iniFileUtils.deleteValue("Demo2");
//        iniFileUtils.deleteValue("Demo1");
//
//        // 保存配置文件
        iniFileUtils.save("C:\\Users\\zhanghg\\Desktop\\config.ini");
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_22019789

谢谢你的支持,我会再接再厉哒

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值