Java 读写ini文件

1 篇文章 0 订阅

引言

记录使用

依赖

<dependency>
       <groupId>org.ini4j</groupId>
       <artifactId>ini4j</artifactId>
       <version>0.5.4</version>
</dependency>

代码

import org.apache.commons.lang3.StringUtils;
import org.ini4j.Ini;
import org.ini4j.Profile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * ini文件util类
 *
 * @author zhujx
 * @date 2021/04/16 17:29
 */
public class IniUtils {

    private static final Logger log = LoggerFactory.getLogger(IniUtils.class);

    private static Ini ini;


    /**
     * 初始化ini文件
     */
    public static void init() {
        init("src/main/resources/my.ini");
    }

    /**
     * 初始化ini文件
     *
     * @param iniPath ini文件路径
     * @author zhujx
     * @date 2021/4/17 13:25
     */
    public static void init(String iniPath) {
        try {
            ini = new Ini(new File(iniPath));
        } catch (IOException e) {
            log.error(e.getMessage(), e);
            ini = null;
        }
    }


    /**
     * 读取ini文件
     *
     * @param sectionKey section的值
     * @param keyList    key数组
     * @return java.util.Map<java.lang.String, java.lang.String>
     * @author zhujx
     * @date 2021/4/17 13:25
     */
    public static Map<String, String> readIni(String sectionKey, List<String> keyList) {
        if (keyList == null || keyList.isEmpty()) {
            return null;
        }
        Profile.Section section = ini.get(sectionKey);
        if (section == null) {
            return null;
        }
        Map<String, String> map = new HashMap<>(16);
        for (String key : keyList) {
            String value = section.get(key);
            map.put(key, value);
        }
        return map;
    }

    /**
     * 读取ini文件
     *
     * @param sectionKey section的值
     * @param key        key
     * @return java.lang.String
     * @author zhujx
     * @date 2021/4/17 13:25
     */
    public static String readIni(String sectionKey, String key) {
        if (StringUtils.isBlank(key)) {
            return null;
        }
        Profile.Section section = ini.get(sectionKey);
        if (section == null) {
            return null;
        }
        return section.get(key);
    }


    /**
     * 添加值
     *
     * @param origin 包含section key value
     * @author zhujx
     * @date 2021/4/17 13:27
     */
    public static void creatIni(Map<String, Map<String, String>> origin) throws IOException {
        //将文件内容保存到ini对象中
        Set<String> sectionKeySet = origin.keySet();
        for (String sectionKey : sectionKeySet) {
            if (StringUtils.isBlank(sectionKey)) {
                continue;
            }
            Map<String, String> keyValue = origin.get(sectionKey);
            Set<String> keySet = keyValue.keySet();
            for (String key : keySet) {
                if (StringUtils.isBlank(key)) {
                    continue;
                }
                String value = keyValue.get(key);
                if (StringUtils.isBlank(value)) {
                    value = "";
                }
                ini.add(sectionKey, key, value);
            }
        }
        //将文件内容保存到文件中
        ini.store();
    }

    /**
     * 添加值
     *
     * @param sectionKey sectionKey
     * @param key        key
     * @param value      value
     * @author zhujx
     * @date 2021/4/17 13:27
     */
    public static void creatIni(String sectionKey, String key, String value) throws IOException {
        //将文件内容保存到ini对象中
        if (StringUtils.isBlank(sectionKey)) {
            return;
        }
        if (StringUtils.isBlank(key)) {
            return;
        }
        Profile.Section section = ini.get(sectionKey);
        if (section != null) {
            String oldValue = section.get(key);
            if (oldValue != null) {
                section.remove(key);
            }
        }
        ini.add(sectionKey, key, value);
        //将文件内容保存到文件中
        ini.store();
    }

    /**
     * 清除ini
     *
     * @author zhujx
     * @date 2021/4/17 13:28
     */
    public static void clean() {
        ini = null;
    }
}

使用

public static void main(String[] args) throws IOException {
        IniUtils.init();
        IniUtils.creatIni("ftp", "host", "192.168.0.176");
        IniUtils.creatIni("ftp", "port", "21");
        IniUtils.creatIni("ftp", "username", "admin");
        IniUtils.creatIni("ftp", "password", "000000");
        String host = IniUtils.readIni("ftp", "host");
        String port = IniUtils.readIni("ftp", "port");
        String username = IniUtils.readIni("ftp", "username");
        String password = IniUtils.readIni("ftp", "password");
        System.out.println(host);
        System.out.println(port);
        System.out.println(username);
        System.out.println(password);
        IniUtils.clean();
    }

192.168.0.176
21
admin
000000

[ftp]
host = 192.168.0.176
port = 21
username = admin
password = 000000

如果你喜欢这篇文章,请点个赞,加个关注吧!!!

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值