Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Firstly, the <code>DataOutputStream</code> in your 2nd example serves no useful purpose<sup>1</sup>. Indeed, if your Strings contain characters that don't fit into 8 bits, the <code>writeBytes(String)</code> method is going to mangle the text. Get rid of it. The Data streams are designed for reading and writing fine-grained binary data. For plain bytes, use a plain (or buffered) Input or Output stream.</p> <p>Secondly, in this specific use-case where you writing the entire output is a single write operation, the BufferedWriter doesn't add any value either.</p> <p>So in this case. you should be comparing:</p> <pre><code> File f = new File("source.htm"); Writer w = new FileWriter(f); w.write("Content"); </code></pre> <p>versus</p> <pre><code> File f = new File("source.htm"); OutputStream os = new FileOutputStream(f); os.write("Content".getBytes()); </code></pre> <p>To my mind, the first version looks simpler and cleaner. And it is best to use the <code>Reader</code> and <code>Writer</code> stacks for text I/O ... because that's what they were designed for. (They take care of the encoding and decoding issues, cleanly and transparently.) </p> <p>You could benchmark them if you really need to know which is faster (on your system!) but I suspect there is not a lot of difference ... and that the first version is faster.</p> <p><sup>1 - I think DataOutputStream has buffering under the covers, but for this use-case, buffering does not help performance.</sup></p> <hr> <p>In use-cases where you are performing multiple (small) writes instead of on big one, there is a significant performance advantage in using a <code>BufferedWriter</code> (or a <code>BufferedOutputStream</code>) instead of an unbuffered writer or stream.</p> <hr> <p>The other point is that both versions of your code is using the platform's default character encoding to encode the output file. It may be more appropriate to use a specific encoding independently of the default, or make this a configuration or command line parameter.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload