title: Which is the best way to create file and write to it in Java
body: <p>I normally use <code>PrintWritter</code> object to create and write to a file, but not sure if its the best in terms of speed and security compare to other ways of creating and writing to a file using other approaches i.e. </p>
  
  <pre><code>Writer writer = new BufferedWriter(
               new OutputStreamWriter(
               new FileOutputStream("example.html"), "utf-8"));   
  writer.write("Something");
  </code></pre>
  
  <h2><code>vs</code></h2>
  
  <pre><code>File file = new File("example.html");
  BufferedWriter output = new BufferedWriter(new FileWriter(file));          
  output.write("Something");
  </code></pre>
  
  <h2><code>vs</code></h2>
  
  <pre><code>File file = new File("example.html");
  FileOutputStream is = new FileOutputStream(file);
  OutputStreamWriter osw = new OutputStreamWriter(is);    
  Writer w = new BufferedWriter(osw);
  w.write("something");
  </code></pre>
  
  <h2><code>vs</code></h2>
  
  <pre><code>PrintWritter pw = new PrintWriter("example.html", "UTF-8");
  pw.write("Something");
  </code></pre>
  
  <p>Also, when to use one over the other; a use case scenario would be appreciated. I'm not asking for how to create and write to file, I know how to do that. Its more of compare and contrast sort of question I'm asking. </p>