File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Character encoding using Java Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Character encoding using Java" Watch "Character encoding using Java" New topic
Author

Character encoding using Java

Jitesh Sinha
Ranch Hand

Joined: Jun 19, 2004
Posts: 144
I need to write a file to my m/c.The file's content should be localized.Data is present in localized languages in database.
I have constructed htmlstring based on that data.but when I open the file(fileNameString) using browser,I get all garbage.

Any pointers?

Here is the code I am using :-

FileOutputStream fos = new FileOutputStream(fileNameString, true);


OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");

osw.write(htmlstring);

osw.close();
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
How have you checked the contents of "htmlstring"? You need to be sure it really is correct.

Assuming "htmlstring" contains the correct characters, encoded in UTF-16 (that's how all Java Strings are encoded), then your code looks as if it would successfully write it to the file using UTF-8 encoding.

When you open a Web site via HTTP, the browser receives various HTTP headers. The "Content-Type" header is important for you. For a UTF-8 file, it would typically be "Content-Type: text/html; charset=UTF-8".

But when you open a local file in a Web browser, there are no HTTP headers (obviously). But you can include an HTML meta element to specify the encoding:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

That might work.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.<br /> <br />#:^P
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Character encoding using Java
 
Similar Threads
how adding text in a file (not rewriting the hole file)
OutputStreamWriter
Split a File According to a String in the File
What is this ��
verify encoding