java-7 - try-with-resources:Java 是否对 .close() 的调用顺序做出任何保证?

标签 java-7 try-with-resources

在 Java 7 中使用 try-with-resources 时,是否可以保证 .close() 的调用顺序?

下面是来自 Oracle 的一些示例代码,展示了此功能:

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

zf.close()writer.close() 都会被调用。订单有保障吗?

最佳答案

in the opposite order of declaration ,从内到外闭合。

关于java-7 - try-with-resources:Java 是否对 .close() 的调用顺序做出任何保证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12136637/

相关文章:

java - 我应该为每一行日志使用 try-with-resources 语句吗?

java - Appengine java.lang.IllegalAccessError 与 ArrayElementNodeProperty

Java Try-with-resource 在 Map 中存储输入流

java - 在 try-with-resources 声明期间抛出异常

java - InputStream 在 spring Controller 中关闭

java - 使用新的 try-with-resources block 在 SQLException 上回滚事务

java - 是否可以调用绝对函数在 TYPE_FORWARD_ONLY 结果集中向前移动行?

java - 通过 ; 分割字符串然后比较值

java - 为什么我的 zip 文件中的图像已损坏?

java - Try With Resources vs Try-Catch