title: PrintWriter closing too early
body: <p>I am using the PrintWriter class to write text from a server to a file.  My program is creating the file, but there is no output.  Can someone please advise me of what is incorrect with this snippet of my code?  Cheers.</p>
  
  <pre><code>public class WriteFile {
          PrintWriter logFileWriter;
  
          public WriteFile() {
  
          }
  
          void startLogFile(String logName) {
  
              File file = new File(logName);
              try {
                  file.createNewFile();
              } catch (IOException e1) {
                  // TODO Auto-generated catch block
                  e1.printStackTrace();
              }
  
              try {
                  logFileWriter = new PrintWriter(file);
              } catch (FileNotFoundException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
          }
  
          void printToFile(String text) {
              logFileWriter.printf(text + "\n");
  
          }
  
          void closeLogFile() {
  
              logFileWriter.close();
              logFileWriter = null;
  
          }
      //} remove this extra bracket which caused building error
  }
  </code></pre>