Try With Resources in Java 7 and Above

Introduction Try with Resources in Java 7 and above allows for automatic closing of resources that implement java.lang.AutoCloseable or the more familiar java.io.Closeable (which extends AutoCloseable) Anyone through a basic Java course will be familiar with this block of code. Open a text file to write data and close it when done. void writeToFileLegacy(String filename) throws IOException { FileOutputStream fos = null; try { fos = new FileOutputStream(filename); fos.write(48); } finally { if (fos != null) { fos.close(); } } } The method attempts to write to a fi...
Read More