Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Unable to Write the file in UTF-8  RSS feed

 
Maneesh Chauahn
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I am unable to write the file in UTF-8 by using the OutputStreamWriter but when i used the "unicode" every thing working fine.

Code :

BufferedWriter oBufferedWriter = new BufferedWriter(new OutputStreamWriter(new
FileOutputStream(theResultFile.toString(), true), "UTF-8"));

oBufferedWriter.write(str)

The String which is specified above only contains the Numeric value and English Characters like this : str = "10000000001 E01"

When code is executed and i opened the file in notepad/textpad it shows the file is in the ANSI

But when i used the "unicode" instead of UTF-8 everything works fine . Means when i open the file in Notepad/TextPad it shows file is in Unicode

Can anyone help Why it is not possible to write the file in UTF-8

Thanks in advance for your prompt
 
Vlado Zajac
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see anything incorrect. Does Notepad show the file incorrectly?

For English letters, numbers and other basic (ascii 0-127) characters UTF-8 encoding is exactly same as ANSI so the same version of the file is in both UFT-8 and ANSI.

Encoding is a property which is not saved with the general text files (only some formats like xml have encoding written into header) so Notepad/Textpad/any program can only guess it. Most text editors (including notepad) allow (or reqire) user to specify the encoding when opening and saving files.

And add some non-ascii characters to the String so you can check if they are properly converted to UFT-8.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!