![](/img/trans.png)
[英]Close a dynamic number of AutoCloseable objects in try-with-resources
[英]Close multiple resources with AutoCloseable (try-with-resources)
我知道,如果资源实现了 AutoCloseable,您尝试传递的资源将自动关闭。 到现在为止还挺好。 但是当我有几个想要自动关闭的资源时,我该怎么办。 套接字示例;
try (Socket socket = new Socket()) {
input = new DataInputStream(socket.getInputStream());
output = new DataOutputStream(socket.getOutputStream());
} catch (IOException e) {
}
所以我知道套接字会被正确关闭,因为它是在 try 中作为参数传递的,但是输入和输出应该如何正确关闭?
通过在括号中声明所有资源,可以将 Try with resources 与多个资源一起使用。 查看文档
来自链接文档的相关代码摘录:
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());
}
}
}
如果您的对象没有实现AutoClosable
( DataInputStream
实现),或者必须在 try-with-resources 之前声明,那么关闭它们的适当位置是在finally
块中,也在链接的文档中提到。
别担心,事情会“正常工作”。 来自Socket 的文档:
关闭此套接字也将关闭套接字的 InputStream 和 OutputStream。
我理解您担心没有在输入和输出对象上显式调用close()
并且实际上通常最好确保所有资源都由try-with-resources
块自动管理,如下所示:
try (Socket socket = new Socket();
InputStream input = new DataInputStream(socket.getInputStream());
OutputStream output = new DataOutputStream(socket.getOutputStream());) {
} catch (IOException e) {
}
这会导致套接字对象“多次关闭”,但这不会造成任何伤害(这是通常建议将close()
所有实现设为幂等的原因之一)。
除了上述答案,这是 Java 9 中添加的改进。
Java 9 try-with-resources 改进了编写代码的方式。 现在您可以在 try 块之外声明变量并直接在 try 块内使用它们。因此,您将获得以下好处。
try-with-resource 我们可以在 Java 9 中这样写吗?
public void loadDataFromDB() throws SQLException {
Connection dbCon = DriverManager.getConnection("url", "user", "password");
try (dbCon; ResultSet rs = dbCon.createStatement().executeQuery("select * from emp")) {
while (rs.next()) {
System.out.println("In loadDataFromDB() =====>>>>>>>>>>>> " + rs.getString(1));
}
} catch (SQLException e) {
System.out.println("Exception occurs while reading the data from DB ->" + e.getMessage());
}
}
这里自动资源管理会自动关闭两个对象 dbCon & rs。
为了更好地理解上述定义用例列表,请找到一些 Java 7 代码。
示例 1:
public void loadDataFromDB() throws SQLException {
Connection dbCon = DriverManager.getConnection("url", "user", "password");
try (ResultSet rs = dbCon.createStatement().executeQuery("select * from emp")) {
while (rs.next()) {
System.out.println("In loadDataFromDB() =====>>>>>>>>>>>> " + rs.getString(1));
}
} catch (SQLException e) {
System.out.println("Exception occurs while reading the data from DB ->" + e.getMessage());
} finally {
if (null != dbCon)
dbCon.close();
}
}
示例 2:
// BufferedReader is declared outside try() block
BufferedReader br = new BufferedReader(new FileReader("C://readfile/input.txt"));
try (BufferedReader inBr = br) {
// ...
}
} catch (IOException e) {
// ...
}
在上面的示例中,您可以查看对象是否不在尝试然后我们需要手动关闭或重新引用它。 同样在 try 块中有多个对象的情况下,它看起来很混乱,即使您在 try 内部声明,也不能在 try 块外部使用。
上面的答案很好,但在某些情况下,尝试使用资源没有帮助。
看看这个代码示例:
private static byte[] getFileBytes(Collection<String> fileContent) throws CustomServiceException {
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(baos))) {
for (String fileLine : fileContent) {
writer.append(fileLine);
writer.newLine();
}
}
return baos.toByteArray();
} catch (IOException e) {
throw new CustomServiceException(SC_INTERNAL_SERVER_ERROR, "Unable to serialize file data.");
}
}
在这个例子中,你不能只使用 try-with-resources 块,因为 writer 必须将输出缓冲区刷新到底层字符流,因此将 writer 放入 try-with-resources 块不会起作用,方法将返回空数组.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.