天天看點

Java 讀寫ini檔案

引言

記錄使用

依賴

<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
           

如果你喜歡這篇文章,請點個贊,加個關注吧!!!