java的try后面跟括号

 

 

例子:

1
2
3
4
5
6
7
try (FileReader reader = new FileReader("data.txt")) {
    ...
}catch (IOException io) {
    ...
}finally{
    ....
}

这个括号在JDK1.7之前是没有的,是1.7的新特性。

括号里的内容支持包括流以及任何可关闭的资源,数据流会在 try 执行完毕后自动被关闭,而不用我们手动关闭了  

原文地址:https://www.cnblogs.com/huhuuu/p/11813442.html