I want to change the entry of a key in a section of an ini file. I use the ini4j library. So I wrote the code below. I can change the entry but there are also other changes which I don't want:
- replacement of ";" with "#" which indicates comment lines
- addition of blank lines between sections and comments
So how can I solve it?
this is what I expected:
[section1]
key1=40
key2=30
[section2]
key1=10
key2=20
;section3
[section3]
key1=10
key2=20
this is the file after editing:
[section1]
key1=40
key2=30
[section2]
key1=10
key2=20
#section3
[section3]
key1=10
key2=20
My code:
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();
}
}
Is there a way to change default comment character?