Java trick: Use an implicit “finally” block to automatically close a stream In Java 7, an implicit finally block will automatically close an output stream. Meaning that, instead of all this: PrintWriter writer = new PrintWriter(targetFile, "UTF-8"); try{ writer.write("Hello World"); writer.close(); } catch(FileNotFoundException | UnsupportedEncodingException e){ // Handle exception } You really only need this: try(PrintWriter writer = new PrintWriter(targetFile, "UTF-8")){ writer.write("Hello World"); } catch(FileNotFoundException | UnsupportedEncodingException e) { // Handle exception } The implicit finally block will automatically close the output stream. Twitter Facebook LinkedIn Previous Next Comments
Sabbatical Week 4 - Cat Ba Island, Hanoi, Da Nang, and Seoul A chill last week of sabbatical in Vietnam and South Korea
Sabbatical Week 3 - Ninh Binh, Hanoi, and Ha Long Bay I spent the third week of my sabbatical exploring northern Vietnam and on a cruise in Ha Long Bay
Comments