您好,欢迎访问一九零五行业门户网

Java 9中的try-with-resources有哪些改进?

try-with-resources 在java 7中引入。使用它的目的是在使用后自动 关闭资源。限制是资源需要在try之前或try语句内部声明,否则会抛出编译错误。
java 9改进了try-with-resources,不再需要在try语句内部声明对象。
在下面的示例中,我们实现了try-with-resources的概念。
示例import java.io.*;public class trywithresourcetest { public static void main(string[] args) throws filenotfoundexception { string line; reader reader = new stringreader("tutorialspoint"); bufferedreader breader = new bufferedreader(reader); try(breader) { while((line = breader.readline()) != null) { system.out.println(line); } } catch(ioexception ioe) { ioe.printstacktrace(); } }}
输出tutorialspoint
以上就是java 9中的try-with-resources有哪些改进?的详细内容。
其它类似信息

推荐信息