title: What is the most effective way to read a file without any leaks in production app using java 7
body: <p>I am using <a href="http://file-leak-detector.kohsuke.org" rel="nofollow noreferrer">this File leak detector</a> to figure out opened files in my production code. I have a website which uses below piece of code during query preparation process from a template file. This template file is closed only half the time, so in general left open FD's is a huge number.</p>
  
  <pre><code>public String getQuery(String templateName, Map&lt;String, String&gt; tmplMap) {
      String content="";
      try (InputStream istream = new FileInputStream(new File(templateName));) {
          content = IOUtils.toString(istream);
  
          // content = IOUtils.toString(UpgradeFeatureUtil.class.getClassLoader().getResourceAsStream(templateName));
          for (String key : tmplMap.keySet())
              content=content.replaceAll(UpgradeConstants._PRE+key.toUpperCase()+UpgradeConstants._SUF, tmplMap.get(key));
  
      } catch (IOException e) {
          e.printStackTrace();
      } catch (Exception e) {
          e.printStackTrace();
      }
      return content; 
  }
  </code></pre>
  
  <p>Template file content</p>
  
  <pre><code>{
      "query": {
          "filtered": {
              "query": {
                  "query_string": {
                      "default_field": "comp",
                      "query": "#%#COMP_ID#%#"
                  }
              },
              "filter": {
                  "bool": {
                      "should": [
                          {
                              "query": {
                                  "match": {
                                      "src_type": "#%#SOURCE#%#"
                                  }
                              }
                          },
                          {
                              "query": {
                                  "match": {
                                      "task_type": "#%#TASKS#%#"
                                  }
                              }
                          }
                      ]
                  }
              }
          }
      },
      "size": 200
  }
  </code></pre>
  
  <p><a href="http://file-leak-detector.kohsuke.org" rel="nofollow noreferrer">File leak detector jar</a> throws an exception at </p>
  
  <pre><code>try (InputStream istream = new FileInputStream(new File(templateName));) { 
  </code></pre>
  
  <p>statement, prior to this statement I was reading the template using <code>getResourceAsStream</code>, but later externalised the template file by putting it inside tomcat/bin/templates folder. </p>
  
  <p><strong>Note:</strong> I am using java 7.</p>
  
  <p><strong>Update</strong></p>
  
  <p>Exception by <a href="http://file-leak-detector.kohsuke.org" rel="nofollow noreferrer">File leak detector</a>, which helps in identifying where/when/who opened files in your JVM.</p>
  
  <pre><code>Opened templates/featuretotask.template by thread:http-bio-19000-exec-8 on Wed Nov 30 22:19:34 IST 2016
      at java.io.FileInputStream.&lt;init&gt;(Unknown Source)
      at com.xxx.UpgradeFeatureUtil.getQuery(UpgradeFeatureUtil.java:61)
  </code></pre>
  
  <p>Please let me know whether I need to close <code>FileInputStream</code> or <code>File</code> explicitly because I am already using try-with-resources Statement which ensures that each resource is closed at the end of the statement, so how is there a possibility of file discriptor left open?</p>