title: Writing to txt file from StringWriter
body: <p>I have a StringWriter variable, <code>sw</code>, which is populated by a FreeMarker template. Once I have populated the <code>sw</code>, how can I print it to a text file?</p>
  
  <p>I have a for loop as follows:</p>
  
  <pre><code>for(2 times)
  {
      template.process(data, sw);
      out.println(sw.toString());
  }
  </code></pre>
  
  <p>Right now, I am just outputting to the screen only. How do I do this for a file? I imagine that with each loop, my <code>sw</code> will get changed, but I want the data from each loop appended together in the file.</p>
  
  <p><strong>Edit:</strong> 
  I tried the code below. When it runs, it does show that the <code>file.txt</code> has been changed, but when it reloads, the file still has nothing in it.</p>
  
  <pre><code>sw.append("CheckText");
  PrintWriter out = new PrintWriter("file.txt");
  out.println(sw.toString());
  </code></pre>