try-with-resources 语句是声明一个或多个资源的try语句,在程序执行完后资源一定会随之被关闭,try-with-resources语句确保每一个声明的资源都会在该语句执行完毕后被关闭。任何实现了java.util.AutoCloseable或者java.io.Closeable接口的对象都可以作为资源。
下面的例子使用BufferedReader,读取文件的数据。这里BufferedReader作为一个资源在程序执行完后一定会被关闭。
static String readFirstLineFromFile(String path) throws IOException { try (BufferedReader br = new BufferedReader(new FileReader(path))) { return br.readLine(); } }在上面的例子中,try-with-resources语句声明的资源是BufferedReader。资源声明紧随在try关键字的括号中。在Java SE7和新的版本中,BufferedReader实现了java.util.AutoCloseable。因为BufferedReader在try-with-resource语句中被声明为一个资源,那么它将被无条件关闭,无论try语句是正常执行完还是出现意外(抛出异常)。
static String readFirstLineFromFileWithFinallyBlock(String path) throws IOException { BufferedReader br = new BufferedReader(new FileReader(path)); try { return br.readLine(); } finally { if (br != null) br.close(); } }然而,在上面的例子中,如果readLine或者close抛出了异常,readFirstLineFromFileWithFinallyBlock方法将会从finally语句块中抛出异常,try块中抛出的异常才会被压制。相比之下,在readFirstLineFromFile方法中,如果try块和try-with-resources语句均抛出异常, 那readFirstLineFromFile将抛出从try块中抛出的异常;try-with-resources块抛出的异常会被压制。
public static void writeToFileZipFileContents(String zipFileName, String outputFileName) throws java.io.IOException { java.nio.charset.Charset charset = java.nio.charset.Charset.forName("US-ASCII"); java.nio.file.Path outputFilePath = java.nio.file.Paths.get(outputFileName); // Open zip file and create output file with try-with-resources statement try ( java.util.zip.ZipFile zf = new java.util.zip.ZipFile(zipFileName); java.io.BufferedWriter writer = java.nio.file.Files.newBufferedWriter(outputFilePath, charset) ) { // Enumerate each entry for (java.util.Enumeration entries = zf.entries(); entries.hasMoreElements();) { // Get the entry name and write it to the output file String newLine = System.getProperty("line.separator"); String zipEntryName = ((java.util.zip.ZipEntry)entries.nextElement()).getName() + newLine; writer.write(zipEntryName, 0, zipEntryName.length()); } } }在这个例子中,try-with-resource语句包含两个资源声明ZipFile和BufferedWriter,用分号隔开。当下面的代码块执行结束,无论是正常执行结束或者抛出了异常,BufferedWriter和ZipFile的close方法会随之被调用。注意,这个两个资源的close方法的调用不一定是按照资源创建的顺序调用执行的。
下面的例子使用try-with-resouce语句自动关闭java.sql.Statement对象:
public static void viewTable(Connection con) throws SQLException { String query = "select COF_NAME, SUP_ID, PRICE, SALES, TOTAL from COFFEES"; try (Statement stmt = con.createStatement()) { ResultSet rs = stmt.executeQuery(query); while (rs.next()) { String coffeeName = rs.getString("COF_NAME"); int supplierID = rs.getInt("SUP_ID"); float price = rs.getFloat("PRICE"); int sales = rs.getInt("SALES"); int total = rs.getInt("TOTAL"); System.out.println(coffeeName + ", " + supplierID + ", " + price + ", " + sales + ", " + total); } } catch (SQLException e) { JDBCTutorialUtilities.printSQLException(e); } }
注意:一个try-with-resource语句就想一般的try语句一样可以有catch和finally语句块。在try-with-resource语句中的catch和finally语句块将在资源关闭后执行。
文章译自:http://docs.oracle.com/javase/7/docs/technotes/guides/language/try-with-resources.html