title: When to flush a BufferedWriter
body: <p>In a Java program (Java 1.5), I have a BufferedWriter that wraps a Filewriter, and I call write() many many times... The resulting file is pretty big...</p>
  
  <p>Among the lines of this file, some of them are incomplete...</p>
  
  <p>Do I need to call flush each time I write something (but I suspect it would be inefficient) or use another method of BufferedWriter or use another class...?</p>
  
  <p>(Since I've a zillion lines to write, I do want to have something quite efficient.)
  What would be the ideal "flushing" moment? (when I reach the capacity of the BufferedWriter)...</p>
  
  <p>Init: </p>
  
  <pre><code>try {
    analysisOutput = new BufferedWriter(new FileWriter(
        "analysisResults", true));
    analysisOutput.newLine();
    analysisOutput.write("Processing File " + fileName + "\n");
  } 
  catch (FileNotFoundException ex) {
    ex.printStackTrace();
  } 
  catch (IOException ex) {
    ex.printStackTrace();
  }
  </code></pre>
  
  <p>Writing:</p>
  
  <pre><code>private void printAfterInfo(String toBeMoved,HashMap&lt;String, Boolean&gt; afterMap, Location location)
    throws IOException {
      if(afterMap != null) {
        for (Map.Entry&lt;String, Boolean&gt; map : afterMap.entrySet()) {
          if (toBeMoved == "Condition") {
            if (1 &lt;= DEBUG)
              System.out.println("###" + toBeMoved + " " + location + " "
                  + conditionalDefs.get(conditionalDefs.size() - 1)
                  + " After " + map.getKey() + " "
                  + map.getValue() + "\n");
  
            analysisOutput.write("###" + toBeMoved + " " + location + " "
                + conditionalDefs.get(conditionalDefs.size() - 1)
                + " After " + map.getKey() + " " + map.getValue()
                + "\n");
          } else {
            if (1 &lt;= DEBUG)
              System.out.println("###" + toBeMoved + " " + location + " "
                  + map.getKey() + " After " 
                  + map.getValue() + "\n");
            if (conditionalDefs.size() &gt; 0)
              analysisOutput.write("###" + toBeMoved + " " + location + " "
                  + conditionalDefs.get(conditionalDefs.size() - 1) + " "
                  + map.getKey() + " After " + map.getValue()
                  + "\n");
            else
              analysisOutput.write("###" + toBeMoved + " " + location + " " + map.getKey() + " After " + map.getValue() + "\n");
  
  
          }
        }
      }
  </code></pre>
  
  <p>I've just figured out that the lines which are incomplete are those just before "Processing file"... so it occurs when I'm switching from one file that I analyze to another...</p>
  
  <p>Closing:</p>
  
  <pre><code>dispatch(unit);
  
  try {
    if (analysisOutput != null) {
      printFileInfo();
      analysisOutput.close();
    }
  } 
  catch (IOException ex) {
    ex.printStackTrace();
  }
  </code></pre>
  
  <p>Sometimes the information printed out by printFileInfo does not appear in the results file...</p>