我想在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();
}
}是否有办法更改默认的注释字符?
发布于 2016-03-08 18:36:50
显然,即使当ini4j可以用;和#读取注释时,当您必须将它们写入ini文件时,它也不会保存这些注释。
如果您检查AbstractFormatter项目的ini4j类,您可以看到在写入时唯一重要的注释运算符是#,如果您能够访问源代码,您可以使用自定义注释运算符更改它,并且应该工作。
abstract class AbstractFormatter implements HandlerBase
{
private static final char OPERATOR = '=';
private static final char COMMENT = '#'; // <--- change this to ';'
...至于空白处,它们只是美学。同样,如果您可以访问源代码,您可以自己编辑它(注释这和这行吗?),但是我想如果您在这个库生成后重新格式化ini文件并删除所有空行,会更容易一些。
https://stackoverflow.com/questions/33258758
复制相似问题