hoat4's solution is very elegant and simple. It works for all sane ini files. However, I have seen many that have un-escaped space characters in the key.
To solve this, I have downloaded and modified a copy of java.util.Properties
. Though this is a little unorthodox, and short-term, the actual mods were but a few lines and quite simple. I will be puting forward a proposal to the JDK community to include the changes.
By adding an internal class variable:
private boolean _spaceCharOn = false;
I control the processing related to scanning for the key/value separation point.
I replaced the space characters search code with a small private method that returns a boolean depending on the state of the above variable.
private boolean isSpaceSeparator(char c) {
if (_spaceCharOn) {
return (c == ' ' || c == '\t' || c == '\f');
} else {
return (c == '\t' || c == '\f');
}
}
This method is used in two places within the private method load0(...)
.
There is also a public method to switch it on, but it would be better to use the original version of Properties
if the space separator is not an issue for your application.
If there is interest, I would be willing to post the code to my IniFile.java
file. It works with either version of Properties
.