<aside> 📎 ITEM9 try-finally 보다는 try-with-resources를 사용하라

</aside>


자원의 닫힘을 보장해주는 수단 : try-finally

static String firstLineOfFile(String path) throws IOException {
	  BufferedReader br = new BufferedReader(new FileReader(path));
	  **try** { return br.readLine(); }
		**finally** { br.close(); }
}
static void copy(String src, String dst) throws IOException {
    InputStream in = new FileInputStream(src);
    **try** { OutputStream out = new FileOutputStream(dst);
        **try** {
            byte[] buf = new byte[BUFFER_SIZE];
            int n;
            while ((n = in.read(buf)) >= 0) out.write(buf, 0, n);
        } **finally** { out.close(); }
    } **finally** { in.close(); }
	}
static String firstLineOfFile(String path) throws IOException {
	  BufferedReader br = new BufferedReader(new FileReader(path));
	  try { return br.**readLine**(); }
		finally { br.**close**(); }
}

해결책의 등장 : try-with-resources

public abstract class BufferedReader implements AutoCloseable { ... }
static String firstLineOfFile(String path) throws IOException {
    try (BufferedReader br = new BufferedReader(new FileReader(path))) {
				return br.readLine();
		}
}