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 @   整合侠  阅读(641)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示