Home arrow static arrow Java Programming [Archive] - OutputStreamWriter does not write UTF-8
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - OutputStreamWriter does not write UTF-8
This topic has 3 replies on 1 page.

Posts:16
Registered: 1/31/01
OutputStreamWriter does not write UTF-8  
Mar 1, 2003 7:09 AM



 
Hi,

I tried to write a class that writes a file in UTF-8. However, when I verify the format in Windows Editor, it always says it's ANSI. Reading UTF-8 files is working fine. I also can write files successfully with "Unicode" as enctype... But UTF8 (which I need) does not work... does anyone perhaps have an idea?

Thanks!
HTW

public class WriteKMSXMLFile {

private File f;
private File d;
private String enc;
private String text = "Test.";

public WriteKMSXMLFile(String filename, String enc) {
this.f = new File(filename);
this.enc = enc;
writeFile();
}

protected void writeFile() {
try
{
FileOutputStream fout = new FileOutputStream(f);
OutputStreamWriter osw = new OutputStreamWriter(fout, enc);
BufferedWriter out = new BufferedWriter(osw);
out.write(text);
out.flush();
out.close();
}
catch (Exception x) { System.out.println(x.getMessage()); }

}

public static void main(String[] args) {
WriteKMSXMLFile writeKMSXMLFile1 = new WriteKMSXMLFile("d:\\xmltest
test.txt", "UTF-8");
}
}
 

Posts:16
Registered: 1/31/01
Re: OutputStreamWriter does not write UTF-8  
Mar 1, 2003 7:18 AM (reply 1 of 3)



 
Problem has been solved!
Thanks.
 

Posts:9
Registered: 12/13/01
Re: OutputStreamWriter does not write UTF-8  
Aug 6, 2004 1:11 AM (reply 2 of 3)



 
and how did you solve it? :O
 

Posts:183
Registered: 5/28/04
Re: OutputStreamWriter does not write UTF-8  
Aug 6, 2004 4:48 AM (reply 3 of 3)



 
Something ike this, I bet.

//Open an Output Stream Writer to set encodingOutputStream fout = new FileOutputStream(fileName);OutputStream bout = new BufferedOutputStream(fout);OutputStreamWriter out = new OutputStreamWriter(bout,"UTF-8");
 
This topic has 3 replies on 1 page.