以前在使用一些资源对象的时候,用完以后都要手动处理关闭或者释放对象,以避免内存溢出。
JDK7出了一个新的方法,try-with-resources 语句:
static String readFirstLineFromFile(String path) throws IOException {
try (BufferedReader br = new BufferedReader(new FileReader(path))) {
return br.readLine();
}
}