{"id":533,"date":"2009-11-15T04:35:47","date_gmt":"2009-11-15T02:35:47","guid":{"rendered":"http:\/\/raftaman.net\/?p=533"},"modified":"2021-05-15T11:46:41","modified_gmt":"2021-05-15T09:46:41","slug":"filewriter-xml-and-utf-8","status":"publish","type":"post","link":"https:\/\/possiblelossofprecision.net\/?p=533","title":{"rendered":"FileWriter, XML and UTF-8"},"content":{"rendered":"<p>Deep down in the Java-API:<\/p>\n<p><a href=\"http:\/\/java.sun.com\/javase\/6\/docs\/api\/java\/io\/FileWriter.html\">http:\/\/java.sun.com\/javase\/6\/docs\/api\/java\/io\/FileWriter.html<\/a><\/p>\n<blockquote><p>Convenience class for writing character files. The constructors of this class assume that the default character encoding and the default byte-buffer size are acceptable. To specify these values yourself, construct an OutputStreamWriter on a FileOutputStream.<\/p><\/blockquote>\n<p>So, if you want to write you XML-Document to a file, for the love of god, <strong>don&#8217;t use the FileWriter<\/strong> like this:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n        BufferedWriter bufout = new BufferedWriter(new FileWriter(OUTFILE));\r\n        bufout.write(out);\r\n        bufout.close();\r\n<\/pre>\n<p>or you might end up with an XML-file that has a UTF-16 header (<code>encoding=\"UTF-16\"<\/code>) but is encoded completely differently (plain ASCII?! Not sure&#8230;).<\/p>\n<p><strong>Insted, use <\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n                OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(OUTFILE),&quot;UTF-16&quot;);\r\n                out.write(s);\r\n                out.close();\r\n<\/pre>\n<p>Resources:<br \/>\n<a href=\"http:\/\/www.malcolmhardie.com\/weblogs\/angus\/2004\/10\/23\/java-filewriter-xml-and-utf-8\/\">http:\/\/www.malcolmhardie.com\/weblogs\/angus\/2004\/10\/23\/java-filewriter-xml-and-utf-8\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Deep down in the Java-API: http:\/\/java.sun.com\/javase\/6\/docs\/api\/java\/io\/FileWriter.html Convenience class for writing character files. The constructors of this class assume that the default character encoding and the default byte-buffer size are acceptable. To specify these values yourself, construct an OutputStreamWriter on a FileOutputStream. So, if you want to write you XML-Document to a file, for the love of god, don&#8217;t use the&#8230; <a href=\"https:\/\/possiblelossofprecision.net\/?p=533\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[4,21],"class_list":["post-533","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-java","tag-utf8"],"_links":{"self":[{"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/posts\/533","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=533"}],"version-history":[{"count":13,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/posts\/533\/revisions"}],"predecessor-version":[{"id":704,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/posts\/533\/revisions\/704"}],"wp:attachment":[{"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=533"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=533"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=533"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}