title: How do I use Buffered streams to append to a file in Java?
body: <p>I have the following code, but I'm not sure that i'm doing  everything correctly in terms of efficiency/flushing/closing streams. Some advice would greatly help, thank you</p>
  
  <pre><code>    OutputStream out = null;
      try {
          out = new BufferedOutputStream(new FileOutputStream(file, true));
          byte[] buf = new byte[32 * 1024]; // should this be 32KB?
          while ((in.read(buf)) &gt; 0) {
              out.write(buf);
          }
          out.flush();
      } catch (Exception e) {
          e.printStackTrace();
      } finally {
          try {
              if (out != null)
                  out.close();
              if (in != null)
                  in.close();
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  </code></pre>