com.neko233.skilltree.commons.configFile.IniFile233 Maven / Gradle / Ivy
package com.neko233.skilltree.commons.configFile;
import lombok.Getter;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* .ini 文件读取
*
* @author SolarisNeko on 2023-10-01
*/
public class IniFile233 {
public static final String REGEX_SECTION = "\\[.*\\]";
// [title] > key = value
@Getter
private final Map> sectionMap = new LinkedHashMap<>();
private Map currentSectionMap = null;
public void reload(String filePath) throws IOException {
File file = new File(filePath);
reload(file);
}
public void reload(File file) throws IOException {
sectionMap.clear();
BufferedReader reader = new BufferedReader(new FileReader(file));
String line;
while ((line = reader.readLine()) != null) {
line = line.trim();
if (line.matches(REGEX_SECTION)) {
// Found a new section = [abc] 不允许空格
String sectionName = line.substring(1, line.length() - 1);
// 新的 section
currentSectionMap = new LinkedHashMap<>();
sectionMap.put(sectionName, currentSectionMap);
} else if (currentSectionMap != null && line.contains("=")) {
// Found a key-value pair within a section
String[] parts = line.split("=", 2);
String key = parts[0].trim();
String value = parts[1].trim();
currentSectionMap.put(key, value);
}
}
reader.close();
}
}