title: Should I close stream when reusing FileOutputStream?
body: <p>As stated in the title, should I close stream when reusing a FileOutputStream variable? For example, in the following codes, should I call the <code>outfile.close()</code> before I assign it a new file and why?</p>
  
  <p>Thanks:)</p>
  
  <pre><code>FileOutputStream outfile = null;
  int index = 1;
  
  while (true) {
  
      // check whether we should create a new file
      boolean createNewFile = shouldCreateNewFile();
  
      //write to a new file if pattern is identified
      if (createNewFile) {
          /* Should I close the outfile each time I create a new file?
          if (outfile != null) {
              outfile.close();
          }
          */
          outfile = new FileOutputStream(String.valueOf(index++) + ".txt");
      }
  
      if (outfile != null) {
          outfile.write(getNewFileContent());
      }
  
      if (shouldEnd()) {
          break;
      }
  }
  
  try {
      if (outfile != null) {
          outfile.close();
      }
  } catch (IOException e) {
      System.err.println("Something wrong happens...");
  }
  </code></pre>