Hello

Im trying the ini4j library but I seem to be having problems writing a Unicode file (no problems reading I believe)

Is this a Java issue or ini4j?

Thank you

Dani AI

Generated

Short answer: this was an encoding + ini4j-escaping interaction rather than a mysterious Java bug. FileReader/FileWriter use the platform default charset unless you explicitly choose another, so they can silently change what bytes get written or read. (docs.oracle.com)

ini4j will also escape characters by default (it emits Java-style \uXXXX escapes when escaping is enabled or when characters cannot be represented in the target encoding). The library exposes that behavior through its Config (escape is true by default). (ini4j.sourceforge.net)

Practical, reliable choices (order by how safe they are):

  • Pick a canonical file encoding (UTF-8 is recommended), open the file with an explicit Reader/Writer that uses that charset, and call Ini.load(Reader) / Ini.store(Writer) so bytes/characters round-trip predictably. ini4j supports load/store with Reader/Writer. (ini4j.sourceforge.net)
  • If you need BOM detection or Windows-style handling, ini4j has a Unicode-aware input reader and BOM support; enable/use those helpers when you need auto-detection. (jar-download.com)
  • If the desired behavior is literal INI text (no \u escapes), either use Wini (its constructor disables escaping by default) or configure a cloned Config and set escape=false for that Ini instance rather than changing global behavior. (jar-download.com)

Notes tied to the thread: 's workaround (writing bytes in an 8-bit Western encoding) works because Latin‑1 contains ñ as a single byte; it hides the escaping symptom but ties the file to that 8‑bit encoding. For modern apps prefer an explicit Unicode encoding (UTF‑8) plus reading/writing via readers/writers, or turn escaping off intentionally when interoperating with other INI consumers.

Quick checklist for debugging: confirm the file's actual bytes (hex), open with the exact charset you intend, prefer Ini.load(Reader)/Ini.store(Writer), and inspect Config.isEscape() / use Wini if you want Windows-style defaults.

Recommended Answers

All 5 Replies

pretty difficult to say where your problem is ...
what do you mean by: seem to be having problems?
what problem? does it give an error message, if so, which one, ...

it's obviously a Java issue, since ini4j is a Java api, but whether the issue is within ini4j or in your code ...

pretty difficult to say where your problem is ...
what do you mean by: seem to be having problems?
what problem? does it give an error message, if so, which one, ...

it's obviously a Java issue, since ini4j is a Java api, but whether the issue is within ini4j or in your code ...

Man I explain things wrong....

Instead of writing in the file

ñ

it puts

\u007a

which I BELIEVE is a ANSI/ASCI code right

Something like

helloñhello

is written as

hello\u007ahello

Anyone have any ideas or tips? I know the FileWriter doesnt support Unicode (dumb on Sun's part) but is there any alternatives? I cant seem to get DataStreamWriter to work correctly with ini4j

Some text and code example:

Here is the contents of the ini name hey.ini lets say:

helloñhello

[section]
x=test

now code:

Ini vini=new Ini();
vini.load(new FileReader("hey.ini"));
String v =vini.get("section").get("x");
System.out.println("The variable contains before saving: " + v);
System.out.println(v.length());
vini.put("section", "x", Integer.parseInt("123"));		
//OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream("hey.ini"),"UTF-8");	
vini.store(new FileOutputStream("hey.ini"));			
FileOutputStream file_output=new FileOutputStream("hey.ini");		 
DataOutputStream data_out=new DataOutputStream(file_output);
vini.store(new OutputStreamWriter(data_out,"UTF-8"));		
//vini.store(new OutputStreamWriter("hey.ini","UTF-8"));			
vini.load(new FileReader("hey.ini"));	
v =vini.get("section").get("x");
System.out.println("The variable contains after saving:: " + v);
System.out.println(v.length());

After running this code, the hey.ini file is:

hello\u007ahello

[section]
x = 123

Fixed.

The correct code is:

Ini vini=new Ini();
vini.load(new FileReader("hey.ini"));
String v =vini.get("section").get("x");
System.out.println("The variable contains before saving: " + v);
System.out.println(v.length());
vini.put("section", "x", Integer.parseInt("123"));		
//OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream("hey.ini"),"ISO-8859-1");	
vini.store(new FileOutputStream("hey.ini"));
FileOutputStream file_output=new FileOutputStream("hey.ini");		 DataOutputStream data_out=new DataOutputStream(file_output);
vini.store(new OutputStreamWriter(data_out,"ISO-8859-1"));
//vini.store(new OutputStreamWriter("hey.ini","ISO-8859-1"));
vini.load(new FileReader("hey.ini"));
v =vini.get("section").get("x");
System.out.println("The variable contains after saving:: " + v);
System.out.println(v.length());
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.