`
jackiehff
  • 浏览: 6087 次
社区版块
存档分类
最新评论

Java SE7新特性之try-with-resources语句

 
阅读更多

try-with-resources语句是一个声明一个或多个资源的try语句。一个资源作为一个对象,必须在程序结束之后随之关闭。try-with-resources语句确保在语句的最后每个资源都被关闭 。任何实现了java.lang.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 SE 7以及后续版本中,BufferedReader类实现了java.lang.AutoCloseable接口。 因为BufferedReader实例是在try-with-resource 语句中声明的, 所以不管try语句正常地完成或是 发生意外(结果就是BufferedReader.readLine方法抛出IOException),BufferedReader都将会关闭

在 Java SE 7之前, 可以使用finally块来确保资源被关闭,不管try语句正常地完成或是发生意外。下面的例子使用finally块替换try-with-resources 语句:

    static String readFirstLineFromFileWithFinallyBlock(String path) throws IOException {
      BufferedReader br = new BufferedReader(new FileReader(path));
      try {
        return br.readLine();
      } finally {
        if (br != null) br.close();
      }
    }

然而,在这个例子中,如果readLineclose方法均抛出异常,那么readFirstLineFromFileWithFinallyBlock方法将抛出从finally块中抛出的异常; try块中抛出的异常被抑制了。与此相反, 在readFirstLineFromFile这个例子中, 如果try块和try-with-resources 语句均抛出异常, 那么readFirstLineFromFile将抛出从try块中抛出的异常; try-with-resources 块抛出的异常被抑制了。在Java SE 7 以及后续的版本中, 你可以检索被抑制的异常;详情参见Suppressed Exceptions

可以在一个try-with-resources 语句中声明一个或多个资源。下面的例子检索zip文件zipFileName中所有文件的名称并创建一个包含那些文件名称的文本文件:

    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-resources 语句包含两个由分号隔开的声明:ZipFileBufferedWriter。当代码块直接伴随着它正常地或由于一个异常终止时,BufferedWriterZipFile对象的close方法以这种顺序自动地调用 。注意:资源的close方法调用顺序与它们的创建顺序相反

下面的例子使用一个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);
      }
    }

这个例子中使用的java.sql.Statement这个资源是JDBC 4.1以及后续版本API的一部分。

注意: 一个try-with-resources 语句可以像普通的try语句那样有catchfinally块。在try-with-resources 语句中, 任意的catch或者finally块都是在声明的资源被关闭以后才运行。

被抑制的异常

try-with-resources 语句关联的代码块可能会抛出异常。在writeToFileZipFileContents这个例子中, 当试图关闭ZipFileBufferedWriter对象时,try块可能会抛出一个异常,并且try-with-resources 语句可能抛出多达两个异常 。如果try块抛出异常并且try-with-resources 语句抛出一个或多个异常,那么从try-with-resources 语句中抛出的异常将会被抑制, 并且块抛出的异常是由writeToFileZipFileContents方法抛出的那一个。你可以通过调用由try块抛出的异常的Throwable.getSuppressed方法检索这些被抑制的异常信息

实现了AutoCloseable 或 Closeable 接口的类

参见AutoCloseableCloseable接口的Javadoc可以看到实现了两者当中的任何一个接口的类集Closeable接口继承了AutoCloseable接口。Closeable接口的close方法抛出IOException类型的异常而AutoCloseable接口的close方法抛出Exception类型的异常因此, subclasses of theAutoCloseable接口的子类可以重写close方法的这个行为来抛出指定的异常,例如IOException, 或者没有异常。


本文翻译自Oracle官方文档http://docs.oracle.com/javase/7/docs/technotes/guides/language/try-with-resources.html,如有不正确的地方,敬请指正,谢谢!

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics