The Java – ini4j store method changes the comment character

I want to change the key entry in part of the INI file I use the ini4j library So I wrote the following code I can change entries, but there are other changes I don't want:

>Replace ";" Use "#" for comment line > Add empty line between part and comment

How can I solve it?

This is my expectation:

[section1]
key1=40
key2=30
[section2]
key1=10
key2=20
;section3
[section3]
key1=10
key2=20

This is the edited file:

[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 any way to change the default comment character?

Solution

Obviously, even if ini4j you can read comments; And #, when you have to write them to the INI file, it will not be saved

If you check the abstractformatter class of the ini4j project, you can see that the only annotation operator when writing is #. If you have access to the source code, you can use the custom annotation operator to change it and it should work

abstract class AbstractFormatter implements HandlerBase
{
    private static final char OPERATOR = '=';
    private static final char COMMENT = '#';    // <--- change this to ';'
    ...

As for empty lines, they are only aesthetics Similarly, you can edit it yourself (comment this and this line?), But if you just reformat your INI file and delete all empty files after the library is generated, I think it will be easier

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>