这样的好处是:在手动释放jdbc连接的时候,需要关闭 Connection,Statement,ResultSet 这样的资源,需要嵌套多个try-catch。
使用方式: 多个语句之间用分号分隔
一个简单的读取文件的例子:
public class TryCatch { public static void main(String[] args) throws Exception { File file = new File("/devlp/file.txt"); try (FileInputStream fis = new FileInputStream(file); InputStreamReader reader = new InputStreamReader(fis); ) { char[] buffer = new char[1024]; int read =0; while((read =reader.read(buffer))!=-1){ System.out.println(new String(buffer,0,read)); } } } }