title: Printwriter creates empty files even after using printwriter.close
body: <p>I am using <code>PrintWriter</code> but it sometimes creates empty files. </p>
  
  <p>I have also used <code>PrintWriter.close()</code> at the end but it still creates empty file sometimes. </p>
  
  <p><strong>Code:</strong></p>
  
  <pre><code>StringBuilder fileContent = new StringBuilder();
  for (Iterator&lt;String&gt; i = fieldMap.values().iterator(); i.hasNext();) 
  {
    String fieldValue = i.next();
    LOG.debug("Write information to outputfile:  fieldValue" + fieldValue);
    fileContent.append(fieldValue);
    if (i.hasNext())
      fileContent.append("~");
  }
  
  // Member errorMessage is only set in case of fatal error and might
  // be appended without risk since in
  // this case this content is only written to logger.
  fileContent.append(errorMessage);
  fileContent.append("\n");
  
  PrintWriter docFileWriter = new PrintWriter(fileName, "UTF-8");
  docFileWriter.write(fileContent.toString());
  docFileWriter.close();
  </code></pre>