How to get rid of spaces when using the ini.store() method to write into ini files?

491 views Asked by At

I am using the Java API ini4j to parse ini files. My original .ini file has the following key-value format:

[section]
key=value

where no spaces are around the = character.

But after after using the ini.store() method to save changes into the file (where ini is the Ini object referencing my .ini file), the content in the .ini file becomes

[section]
key = value

where extra spaces are inserted around the = character. I am not sure if I am using the store() method correctly. Is there a way to get rid of the spaces?

---- Update ----

According to this answer, I have inserted the following lines in my code:

Config config = new Config();
config.setStrictOperator(true);
ini.setConfig(config);

The spaces are removed. But I have a new problem with the semicolons:

# before
[bashful]
weight = 45.7
height = 98.8
age = 67
homePage = http://snowwhite.tale/~bashful
homeDir = /home/bashful

# after
[bashful]
weight=45.7
height=98.8
age=67
homePage=http\\\://snowwhite.tale/~bashful
homeDir=/home/bashful
2

There are 2 answers

0
Peng On

Finally resolved the problem. Post my own solution for future reference:

To get rid of spaces before and after =, need to use:

Config config = ini.getConfig(); // instead of Config config = new Config()  
config.setStrictOperator(true);
ini.setConfig(config); 

Make sure you are using Wini, not Ini. If you use Ini and config.setStrictOperator(true), the problem is that you will see extra back-slashes inserted before some special characters such as :, ", etc. Simple fix is to change Ini to Wini:

Wini ini = new Wini(new File(filename));
0
FakeAlcohol On

You can set escape to false when using Ini4j on Linux to resolve it:

Config.getGlobal().setEscape(false);
Config.getGlobal().setStrictOperator(true);
Ini ini=new Ini(yourFile);