title: Java equivalent of C# 'using' statement
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/2943542/using-keyword-in-java">&ldquo;using&rdquo; keyword in java</a>  </p>
  </blockquote>
  
  
  
  <p>I'm transitioning from C# to java, so please bear with me...</p>
  
  <p>When reading a file in C#, you simply wrap it all in a big 'using' block, so that if you have an exception, the file will still be closed. Like so (maybe inaccurate but you get the idea):</p>
  
  <pre><code>using(FileStream fs = new FileStream("c:\\myfile.txt")) {
    // Any exceptions while reading the file here won't leave the file open
  }
  </code></pre>
  
  <p>Is there a convenient equivalent in java 5 or 6? I get the impression that lately java has been 'borrowing' some of the syntactic sugar from c# (such as foreach) and so i wouldn't be surprised if there's a java equivalent of using these days.</p>
  
  <p>Or do i just have to use a try..finally block? 'Using' is just so much nicer i think...</p>