3

I am writing a Java application that I just need it to make a change to one key setting within an ini file. I am using ini4j to do this, but have come across some issue when loading the file.

The file looks like what I have here below:

[SECTION 1]
#Some Commented text
Key1=value 1 for key 1
+value 2 for key 1
+value 3 for key 1

Key2=value for key 2<BR>
Key3=value for key 3<BR>

Whats happening is that when my app goes to change one setting (key 3 for example), it changes the entire file and I get what I have here below:

[SECTION 1]
#Some Commented text
Key1=value 1 for key 1
+value 2 for key 1=
+value 3 for key 1=

Key2=value for key 2
Key3=new value 1 for key 3
+new value 2 for key 3

Notice that ini4j recognizes my multivalued keys on new lines as key settings and places an = sign at the end of it, as well as changes all of the commenting from ; to # (which shouldn't cause an issue but would prefer that nothing else but the key I specify be changed).

I was able to isolate the problem happening right when I load the ini file using the code snippet I have below:

String iniFileLoc = "my path\\iniFile.ini";

    String valueLine = "value 1 for key 3\n" + "+value 2 for key 3\n";
    Wini ini = new Wini();
    File iniFile = new File(iniFileLoc);
    ini.load(iniFile);


        ini.add("SECTION1", "Key3", valueLine);

        ini.store(iniFile);

Is there any way to keep ini4j from changing anything else but the specific key i spcify? If not, how can I keep ini4j from recognizing uncommented lines as necessarily being key settings. The ini file I am working with is required to be in this format so i'm not able to specify another method of having multivalued keys other than using the + signs.

Any help would be greatly appreciated.

Thanks

alex-ayala
  • 73
  • 9
  • What you're using may look like an ini file and have that extension, but it isn't one. Ini files have three parts: Sections, defined with `[]`, key/value pairs (`key=value`), and comments (lines starting with `;`). There are no multi-line values marked by `+` and no comments beginning with `#` in the ini file format. – Ken White Oct 18 '12 at 13:45
  • So I'm guessing the only way around this would have to be using a buffered reader to identify the key I need to modify and make changes accordingly? – alex-ayala Oct 18 '12 at 17:08
  • I can't offer a solution (I'm not a Java person), which is why I only posted a comment. :-) I was speaking to the format of the file itself, and why you were probably getting output different than you expected. – Ken White Oct 18 '12 at 17:11

0 Answers0