java7新特性之Try-with-resources statement

try-with-resources 语句是一个声明了1到多个资源的try语句。资源是指这个try执行完成后必需close掉的对象,比如connection, resultset等。

try-with-resources 语句会确保在try语句结束时关闭所有资源。实现了java.lang.AutoCloseablejava.io.Closeable的对象都可以做为资源。

下面是一个例子,它会从一个文件中读出首行文本,这里使用了BufferedReader 的实例来读取数据,BufferedReader 是一个资源,它应该在程序完成时被关闭。

static String readFirstLineFromFile(String path) throws IOException {
   try (BufferedReader br = new BufferedReader(new FileReader(path))) {
      return br.readLine();
   }
}

在这个例子里面,资源是一个BufferedReader, 声明语句是在try后面的括号内。在java7或更晚的版本中,BufferedReader实现了java.lang.AutoCloseable接口。由于BufferedReader被定义在try-with-resource 语句中,因此不管try代码块是正常完成或是出现异常,这个BufferedReader 的实例都将被关闭。

在java7之前的版本中,你可以使用finally 代码块来确保资源被关闭(不管try正常完成还是出现异常)。下面是使用finally的例子:

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代码块里面的异常,也就是close方法出现的异常,try代码块里面的异常被禁止;相反,在readFirstLineFromFile这个例子中,如果try 代码块和try-with-resources 语句都出现异常,readFirstLineFromFile 方法将出抛出来自try代码块的异常,从try-with-resources抛出的异常被禁止。在java7或更晚的版本中,我们可以获取到这些被禁止的异常。

你可以声明1到多个资源,看下面的例子

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());
      }
    }
  }


 

在这个例子中,有两个资源,资源之间用分号隔开。资源被关闭的顺序与它们被创建的顺序相反,也就是说writer 先被关闭,接着是zf。

 下面我们使用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 是JDBC4.1或更晚的API的一部分。

注意:try-with-resources 也可以有catch和finally语句块,就像使用一个普通的try语句一样。在try-with-resources 语句中,catch或者finally将在资源被关闭后执行。

  • 15
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值