Skip to content

Instantly share code, notes, and snippets.

@why-jay
Created December 10, 2014 13:05
Show Gist options
  • Save why-jay/0a31ac34200667464469 to your computer and use it in GitHub Desktop.
Save why-jay/0a31ac34200667464469 to your computer and use it in GitHub Desktop.
Resource release in finally
// borrowed from docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
static String readFirstLineFromFileWithFinallyBlock(String path) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(path));
try {
return br.readLine();
} finally {
if (br != null) br.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment