title: Best coding practice for closing of FileOutputStream at various location
body: <pre><code>properties.storeToXML(new FileOutputStream(new File(m_wcontext.getProject().getBaseDirectory() +
           "/" + m_wcontext.getServiceName() + ".properties")),null);
  </code></pre>
  
  <p>I have a similar method call at 10 different places. During the code review it was suggested that I need to close the resource. The recommended method was to use modify the code as below: but that will make the code clumsy as I need to repeat the code 10 times.            </p>
  
  <pre><code>try {
      fios = new FileOutputStream(new File(m_wcontext.getProject().getBaseDirectory() +
                  "/" + m_wcontext.getServiceName() + ".properties"));
      properties.storeToXML(fios, null);
  } finally {
      if(fios!=null) {
           fios.close();
      }
  }
  </code></pre>
  
  <p>Will the below approach is fine. is there anything better?</p>
  
  <pre><code>    FileOutputStream fios = getFileOutPutStream(m_wcontext.getProject().getBaseDirectory()  +  "/" + m_wcontext.getServiceName() + ".properties");        
      properties.storeToXML(fios, null);
  
      // ...      
  
  private FileOutputStream getFileOutPutStream(String path) throws IOException {
      FileOutputStream fios=null;
      try {
          fios = new FileOutputStream(new File(m_wcontext.getProject().getBaseDirectory() +
                  "/" + m_wcontext.getServiceName() + ".properties"));
          properties.storeToXML(fios, null);
      } finally {
          if(fios!=null) {
              fios.close();
          }
      }
      return fios;
  }
  </code></pre>