The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
The try
-with-resources statement is a try
statement that declares one or more resources.try
-with-resources语句是声明一个或多个资源的try语句。A resource is an object that must be closed after the program is finished with it.资源是一个必须在程序完成后关闭的对象。The try
-with-resources statement ensures that each resource is closed at the end of the statement.try
-with-resources语句确保在语句末尾关闭每个资源。Any object that implements 任何实现java.lang.AutoCloseable
, which includes all objects which implement java.io.Closeable
, can be used as a resource.java.lang.AutoCloseable
的对象(包括实现java.io.Closeable
的所有对象)都可以用作资源。
The following example reads the first line from a file.下面的示例从文件中读取第一行。It uses an instance of 它使用BufferedReader
to read data from the file.BufferedReader
实例从文件中读取数据。BufferedReader
is a resource that must be closed after the program is finished with it:BufferedReader
是程序完成后必须关闭的资源:
static String readFirstLineFromFile(String path) throws IOException { try (BufferedReader br = new BufferedReader(new FileReader(path))) { return br.readLine(); } }
In this example, the resource declared in the 在本例中,try
-with-resources statement is a BufferedReader
.try
-with-resources语句中声明的资源是BufferedReader
。The declaration statement appears within parentheses immediately after the 声明语句出现在try
keyword.try
关键字后面的括号内。The class Java SE 7及更高版本中的BufferedReader
, in Java SE 7 and later, implements the interface java.lang.AutoCloseable
.BufferedReader
类实现了接口Java.lang.AutoCloseable
。Because the 由于BufferedReader
instance is declared in a try
-with-resource statement, it will be closed regardless of whether the try
statement completes normally or abruptly (as a result of the method BufferedReader.readLine
throwing an IOException
).BufferedReader
实例是在try
-with-resource语句中声明的,因此无论try
语句是正常完成还是突然完成(由于BufferedReader.readLine
方法引发IOException
),它都将关闭。
Prior to Java SE 7, you can use a 在Java SE 7之前,您可以使用finally
block to ensure that a resource is closed regardless of whether the try
statement completes normally or abruptly.finally
块来确保资源关闭,而不管try
语句是正常完成还是突然完成。The following example uses a 以下示例使用finally
block instead of a try
-with-resources statement:finally
块而不是try
-with-resources语句:
static String readFirstLineFromFileWithFinallyBlock(String path) throws IOException { BufferedReader br = new BufferedReader(new FileReader(path)); try { return br.readLine(); } finally { br.close(); } }
However, in this example, if the methods 但是,在本例中,如果readLine
and close
both throw exceptions, then the method readFirstLineFromFileWithFinallyBlock
throws the exception thrown from the finally
block; the exception thrown from the try
block is suppressed.readLine
和close
方法都抛出异常,那么readFirstLineFromFileWithFinallyBlock
方法抛出finally
块抛出的异常;从try
块引发的异常被抑制。In contrast, in the example 相反,在示例readFirstLineFromFile
, if exceptions are thrown from both the try
block and the try
-with-resources statement, then the method readFirstLineFromFile
throws the exception thrown from the try
block; the exception thrown from the try
-with-resources block is suppressed.readFirstLineFromFile
中,如果从try
块和try
-with-resources语句抛出异常,则方法readFirstLineFromFile
抛出从try
块抛出的异常;从try
-with-resources块引发的异常被抑制。In Java SE 7 and later, you can retrieve suppressed exceptions; see the section Suppressed Exceptions for more information.在JavaSE7和更高版本中,您可以检索被抑制的异常;有关更多信息,请参阅抑制异常一节。
You may declare one or more resources in a 您可以在try
-with-resources statement.try
-with-resources语句中声明一个或多个资源。The following example retrieves the names of the files packaged in the zip file 以下示例检索打包在zip文件zipFileName
and creates a text file that contains the names of these files:zipFileName
中的文件名,并创建包含这些文件名的文本文件:
public static void writeToFileZipFileContents(String zipFileName, String outputFileName) throws java.io.IOException { java.nio.charset.Charset charset = java.nio.charset.StandardCharsets.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()); } } }
In this example, the 在本例中,try
-with-resources statement contains two declarations that are separated by a semicolon: ZipFile
and BufferedWriter
.try
-with-resources语句包含两个用分号分隔的声明:ZipFile
和BufferedWriter
。When the block of code that directly follows it terminates, either normally or because of an exception, the 当直接跟随它的代码块终止时,无论是正常终止还是由于异常而终止,close
methods of the BufferedWriter
and ZipFile
objects are automatically called in this order.BufferedWriter
和ZipFile
对象的close
方法都会按此顺序自动调用。Note that the 请注意,资源的close
methods of resources are called in the opposite order of their creation.close
方法的调用顺序与其创建顺序相反。
The following example uses a 以下示例使用try
-with-resources statement to automatically close a java.sql.Statement
object:try
-with-resources语句自动关闭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); } }
The resource 本例中使用的资源java.sql.Statement
used in this example is part of the JDBC 4.1 and later API.java.sql.Statement
是JDBC 4.1及更高版本API的一部分。
Note: A 注意:try
-with-resources statement can have catch
and finally
blocks just like an ordinary try
statement.try
-with-resources语句可以像普通try
语句一样具有catch
和finally
块。In a 在try
-with-resources statement, any catch
or finally
block is run after the resources declared have been closed.try
-with-resources语句中,任何catch
或finally
块都会在声明的资源关闭后运行。
An exception can be thrown from the block of code associated with the 可以从与try
-with-resources statement.try
-with-resources语句关联的代码块中引发异常。In the example 在示例writeToFileZipFileContents
, an exception can be thrown from the try
block, and up to two exceptions can be thrown from the try
-with-resources statement when it tries to close the ZipFile
and BufferedWriter
objects.writeToFileZipFileContents
中,可以从try
块引发异常,当try
-with-resources语句尝试关闭ZipFile
和BufferedWriter
对象时,最多可以从try
-with-resources语句引发两个异常。If an exception is thrown from the 如果从try
block and one or more exceptions are thrown from the try
-with-resources statement, then those exceptions thrown from the try
-with-resources statement are suppressed, and the exception thrown by the block is the one that is thrown by the writeToFileZipFileContents
method.try
块引发异常,并且从try
-with-resources语句引发一个或多个异常,则从try
-with-resources语句引发的异常将被抑制,并且该块引发的异常是writeToFileZipFileContents
方法引发的异常。You can retrieve these suppressed exceptions by calling the 通过从Throwable.getSuppressed
method from the exception thrown by the try
block.try
块抛出的异常调用Throwable.getSuppressed
方法,可以检索这些被抑制的异常。
See the Javadoc of the 请参阅AutoCloseable
and Closeable
interfaces for a list of classes that implement either of these interfaces.AutoCloseable
接口和Closeable
接口的Javadoc,了解实现这两个接口的类的列表。The Closeable
interface extends the AutoCloseable
interface.Closeable
接口扩展了AutoCloseable
接口。The close
method of the Closeable
interface throws exceptions of type IOException
while the close
method of the AutoCloseable
interface throws exceptions of type Exception
.Closeable
接口的close
方法引发IOException
类型的异常,而AutoCloseable
接口的close
方法引发Exception
类型的异常。Consequently, subclasses of the 因此,AutoCloseable
interface can override this behavior of the close
method to throw specialized exceptions, such as IOException
, or no exception at all.AutoCloseable
接口的子类可以重写close
方法的这种行为,以抛出专门的异常,例如IOException
,或者根本不抛出异常。