Java:读取和更改ini内容

Java:读取和更改ini内容

ini文件:

[system]
records = 2

1、读取ini内容:

import org.ini4j.Wini;
import cn.hutool.core.io.FileUtil;
  /**
     * readIniFile.
     *
     * @param iniFile the iniFile
     * @param section the section
     * @param key     the key
     * @return String
     */
    public static int getRecodeFromIniFile(File iniFile, String section, String key) throws IOException {
        int recode = 0;
        Wini ini = new Wini(new File(iniFile.getPath()));
        String field = ini.get(section, key);
        if (field != null) {
            recode = Integer.parseInt(field);
        }
        return recode;
    }

    int record = 0;
    if (FileUtil.exist("E:/test.ini")) {
      record = getRecodeFromIniFile(new File("E:/test.ini"), "system", "records");
    }

 

2、更新ini内容:

  /**
     * updateIniFile.
     *
     * @param iniFile the iniFile
     * @param section the section
     * @param key     the key
     * @param record  the record
     */
    private void updateIniFile(File iniFile, String section, String key, int record) throws IOException {
        Wini ini = new Wini(new File(iniFile.getPath()));
        ini.put(section, key, record);
        ini.store();
    }

 

posted @ 2023-05-19 16:19  整合侠  阅读(603)  评论(0编辑  收藏  举报