DataOutputStream#writeBytes(String) vs BufferedWriter#write(String)

Java Beginner picture Java Beginner · Dec 3, 2013 · Viewed 8.2k times · Source

I would like to create a HTML file for my report. The content in the report can be created either by using BufferedWriter#write(String)

File f = new File("source.htm");
BufferedWriter bw = new BufferedWriter(new FileWriter(f));
bw.write("Content");

or by using DataOutputStream#writeBytes(String)

File f = new File("source.htm");
DataOutputStream dosReport = new DataOutputStream(new FileOutputStream(f)); 
dosReport.wrtiteBytes("Content");

Is one of them better than the other? Why is it so?

Answer

Ian Roberts picture Ian Roberts · Dec 3, 2013

If you're writing out text then you should use a Writer, which handles the conversion from unicode characters (Java's internal representation of strings) into an appropriate character encoding such as UTF-8. DataOutputStream.writeBytes simply outputs the low-order eight bits of each char in the string and ignores the high-order eight bits entirely - this is equivalent to UTF-8 for ASCII characters with codes below 128 (U+007F and below) but almost certainly wrong for anything beyond ASCII.

Rather than a FileWriter, you should use an OutputStreamWriter so you can select a specific encoding (FileWriter always uses the platform's default encoding, which varies from platform to platform):

File f = new File("source.htm");
BufferedWriter bw = new BufferedWriter(
  new OutputStreamWriter(new FileOutputStream(f), "UTF-8"));
bw.write("Content");