try(ZipOutputStreamzos=newZipOutputStream(response.getOutputStream())){// 使用资源}catch(Exceptione){// 处理异常e.printStackTrace();}该写法是Java 7引入的 “try-with-resources” 写法,也叫自动资源管理(ARM, Automatic Resource Management)。
它的作用是可以自动关闭需要在使用后关闭的资源(如文件、数据库连接、网络连接等)。
例如:
- FileInputStream
- FileOutputStream
- BufferedReader
- ZipOutputStream
- Connection(JDBC)
比如ZipOutputStream是一个IO流,用完必须close(),该写法可以在try结束后自动调用close(),省去finally的代码。