import java.io.*;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* GH
* 新增配置项
* 修改配置项
* 删除配置项
* 查询配置信息
*
*/
public class IniFileUtils {
private Map<String, Map<String, String>> sections = new LinkedHashMap<>();
public IniFileUtils(String filePath) {
load(filePath);
}
private void load(String filePath) {
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
String currentSection = null;
while ((line = reader.readLine()) != null) {
line = line.trim();
// 忽略空行和注释
if (line.isEmpty() || line.startsWith(";") || line.startsWith("#")) {
continue;
}
// 处理节名 [section]
if (line.startsWith("[") && line.endsWith("]")) {
currentSection = line.substring(1, line.length() - 1).trim();
continue;
}
// 处理键值对
int separatorIndex = line.indexOf("=");
if (separatorIndex == -1) {
continue;
}
String key = line.substring(0, separatorIndex).trim();
String value = line.substring(separatorIndex + 1).trim();
// 将键值对添加到当前节中
if (currentSection != null) {
Map<String, String> section = sections.computeIfAbsent(currentSection, k -> new HashMap<>());
section.put(key, value);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 获取配置项具体值
* @param section
* @param key
* @return
*/
public String getValue(String section, String key) {
Map<String, String> sectionMap = sections.get(section);
if (sectionMap != null) {
return sectionMap.get(key);
}
return null;
}
/**
* 新增配置项
* @param section
* @param key
* @param value
*/
public void setValue(String section, String key, String value) {
Map<String, String> sectionMap = sections.computeIfAbsent(section, k -> new HashMap<>());
sectionMap.put(key, value);
}
/**
* 删除配置项
* @param section
*/
public void deleteValue(String section) {
Map<String, String> sectionMap = sections.computeIfAbsent(section, k -> new HashMap<>());
// sectionMap.remove(section);
sections.remove(section);
}
public void save(String filePath) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
for (Map.Entry<String, Map<String, String>> sectionEntry : sections.entrySet()) {
String section = sectionEntry.getKey();
writer.write("[" + section + "]");
writer.newLine();
Map<String, String> sectionMap = sectionEntry.getValue();
for (Map.Entry<String, String> entry : sectionMap.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
writer.write(key + "=" + value);
writer.newLine();
}
writer.newLine();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
IniFileUtils iniFileUtils = new IniFileUtils("C:\\Users\\dem0\\Desktop\\config.ini");
// 读取配置文件
// String value1 = iniFileUtils.getValue("Demo1", "a");
// System.out.println("Value1: " + value1);
//
// String value2 = iniFileUtils.getValue("Demo2", "a");
// String value3 = iniFileUtils.getValue("Demo2", "b");
// System.out.println("Value2: " + value2);
// System.out.println("Value3: " + value3);
// 设置配置文件(新增和修改
iniFileUtils.setValue("Demo1", "a", "1101");
iniFileUtils.setValue("Demo1", "b", "111");
iniFileUtils.setValue("Demo1", "c", "112");
iniFileUtils.setValue("Demo2", "d", "220");
iniFileUtils.setValue("Demo3", "e", "1234");
// 删除配置项
iniFileUtils.deleteValue("Demo3");
iniFileUtils.deleteValue("Demo2");
// iniFileUtils.deleteValue("Demo1");
//
// // 保存配置文件
iniFileUtils.save("C:\\Users\\zhanghg\\Desktop\\config.ini");
}
}