Cleanup resource automatically with Project Lombok
When we use a resource like InputStream, OutputStream in Java, we should remember to close that resource after using. Before Java 7, we can close a resource in a finally block as below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package com.huongdanjava.lombok; import java.io.*; public class Example { public static void main(String[] args) throws IOException { InputStream is = null; try { is = new FileInputStream(new File("/Users/Khanh/text.txt")); } finally { if (is != null) { is.close(); } } } } |
Since Java 7, we can use the try with resource to… Read More