Java9中的Try-With-Resources语句有哪些改进
Java9改进了try-with-resources,
在下面的示例中,我们实现了“try-with-resources”的概念。
示例
import java.io.*; public class TryWithResourceTest { public static void main(String[] args) throws FileNotFoundException { String line; Reader reader = new StringReader("nhooo.com"); BufferedReader breader = new BufferedReader(reader); try(breader) { while((line = breader.readLine()) != null) { System.out.println(line); } } catch(IOException ioe) { ioe.printStackTrace(); } } }
输出结果
nhooo.com