title: Use try-with-resource to read a text file line by line in java
body: <p>For example, here is one way to read a text file line by line:</p>
  
  <pre><code>ArrayList&lt;String&gt; fidiidList = new ArrayList&lt;String&gt;();
  String sampleLine;
  
  String fileStem = "/tmp/x.txt";
  File sampleFile = new File(fileStem);
  BufferedReader sampleBuffer = new BufferedReader(new FileReader(sampleFile));
  while ((sampleLine = sampleBuffer.readLine()) != null) {
      fidiidList.add(sampleLine);
  }
  sampleBuffer.close();
  </code></pre>
  
  <p>You have to close the buffer manually. I am wondering if it's ok to do it this way:</p>
  
  <pre><code>try(BufferedReader sampleBuffer = new BufferedReader(new FileReader(sampleFile))) {
      while ((sampleLine = sampleBuffer.readLine()) != null) {
          fidiidList.add(sampleLine);
      }
  } catch (Exception e) {
      e.printStackTrace();
  }
  </code></pre>
  
  <p>Which according to my textbook promises to close objects automatically.</p>
  
  <p>BTW, when I type this in my IDE, it reminds me to bring language level from 1.8 to 1.7, does this mean Java 8 stopped supporting this feature?</p>