我想更改 ini 文件部分中的键条目。我使用 ini4j 库。所以我写了下面的代码。我可以更改条目,但还有其他我不想要的更改:
- 替换“;” 用“#”表示注释行
- 在部分和评论之间添加空行
那么我该如何解决呢?
这是我的预期:
[section1]
key1=40
key2=30
[section2]
key1=10
key2=20
;section3
[section3]
key1=10
key2=20
这是编辑后的文件:
[section1]
key1=40
key2=30
[section2]
key1=10
key2=20
#section3
[section3]
key1=10
key2=20
我的代码:
public static void setEntry(String filePath, String fileName, String sectionName, String keyName, String entry)
throws IOException {
String path = filePath.concat(fileName);
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(path);
Ini ini = new Ini(inputStream);
ini.getConfig().setStrictOperator(true);
Section section = ini.get(sectionName);
if (section != null) {
if (section.containsKey(keyName)) {
section.put(keyName, entry);
}
else {
section.add(keyName, entry);
}
}
else {
section = ini.add(sectionName);
section.add(keyName, entry);
}
File iniFile = new File(path);
ini.store(iniFile);
}
catch (IOException e) {
e.printStackTrace();
}
finally {
inputStream.close();
}
}
有没有办法改变默认的评论字符?