java如何编辑ini
读取 INI 文件
使用 java.util.Properties 类加载 INI 文件内容。INI 文件通常以键值对形式存储,与 Properties 格式兼容。
Properties props = new Properties();
try (InputStream input = new FileInputStream("config.ini")) {
props.load(input);
} catch (IOException ex) {
ex.printStackTrace();
}
修改 INI 文件内容
通过 setProperty 方法更新或添加键值对,修改后需重新写入文件。
props.setProperty("key", "newValue");
try (OutputStream output = new FileOutputStream("config.ini")) {
props.store(output, "Updated configuration");
} catch (IOException ex) {
ex.printStackTrace();
}
使用第三方库(推荐)
若需处理复杂 INI 结构(如节 Sections),可使用 org.ini4j 库。
添加依赖(Maven):
<dependency>
<groupId>org.ini4j</groupId>
<artifactId>ini4j</artifactId>
<version>0.5.4</version>
</dependency>
读写示例:

Wini ini = new Wini(new File("config.ini"));
ini.put("section", "key", "value"); // 修改或添加值
ini.store(); // 保存更改
注意事项
- 简单键值对优先使用
Properties,复杂结构(含节)选择ini4j。 - 写入文件时会覆盖原有内容,建议备份重要配置。
- 路径需使用绝对路径或确保工作目录正确。






