如果我们使用 try-with-resource 是否需要关闭资源
Is there a need to close resource if we use try-with-resource
我在我的代码中使用了 try-with-resource 块,想知道是否需要在方法结束时关闭资源,或者不需要?
try (S3Object object = s3.getObject(new GetObjectRequest(bucketName, key));
BufferedReader br = new BufferedReader(new InputStreamReader(object.getObjectContent()));
BufferedWriter bw = new BufferedWriter(new FileWriter(new File("output.txt")))){
String line;
while((line=br.readLine())!=null){
bw.write(line);
bw.newLine();
bw.flush();
}
}
没有
The try-with-resources statement ensures that each resource is closed at the end of the statement. Any object that implements java.lang.AutoCloseable, which includes all objects which implement java.io.Closeable, can be used as a resource.
如果您使用的是 java 6 或更早版本:
Prior to Java SE 7, you can use a finally block to ensure that a resource is closed regardless of whether the try statement completes normally or abruptly.
更新:
You may declare one or more resources in a try-with-resources statement.
正如您在代码中使用的那样。
不,你不知道。我们来看一个 try-catch-finally
和 try-with-resource
的例子
Scanner scanner = null;
try {
scanner = new Scanner(new File("test.txt"));
while (scanner.hasNext()) {
System.out.println(scanner.nextLine());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (scanner != null) {
scanner.close();
}
}
这是您的常规 try-catch-finally
,您将关闭 finally
块中的扫描器。现在让我们来看看 try-with-resource
try (Scanner scanner = new Scanner(new File("test.txt"))) {
while (scanner.hasNext()) {
System.out.println(scanner.nextLine());
}
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
}
您不需要在此处关闭 scanner
,因为它会在 try
块执行完毕后自行关闭。如需更多参考,请访问此 blog
您不必关闭 您在 try 子句中定义的资源。但是,根据你的例子,你在 body 的 try:
中也有这个
BufferedWriter bw = new BufferedWriter(new FileWriter(new File("output.txt"))))
并且您的代码没有关闭该资源。这是错误的(保持文件系统句柄打开很可能是真正的资源泄漏)。
换句话说:您可能想将 bw
添加到您的 try-with-resources 子句中,因此它与 S3Object object
的定义一致(请参阅 了解示例 ).
我在我的代码中使用了 try-with-resource 块,想知道是否需要在方法结束时关闭资源,或者不需要?
try (S3Object object = s3.getObject(new GetObjectRequest(bucketName, key));
BufferedReader br = new BufferedReader(new InputStreamReader(object.getObjectContent()));
BufferedWriter bw = new BufferedWriter(new FileWriter(new File("output.txt")))){
String line;
while((line=br.readLine())!=null){
bw.write(line);
bw.newLine();
bw.flush();
}
}
没有
The try-with-resources statement ensures that each resource is closed at the end of the statement. Any object that implements java.lang.AutoCloseable, which includes all objects which implement java.io.Closeable, can be used as a resource.
如果您使用的是 java 6 或更早版本:
Prior to Java SE 7, you can use a finally block to ensure that a resource is closed regardless of whether the try statement completes normally or abruptly.
更新:
You may declare one or more resources in a try-with-resources statement.
正如您在代码中使用的那样。
不,你不知道。我们来看一个 try-catch-finally
和 try-with-resource
Scanner scanner = null;
try {
scanner = new Scanner(new File("test.txt"));
while (scanner.hasNext()) {
System.out.println(scanner.nextLine());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (scanner != null) {
scanner.close();
}
}
这是您的常规 try-catch-finally
,您将关闭 finally
块中的扫描器。现在让我们来看看 try-with-resource
try (Scanner scanner = new Scanner(new File("test.txt"))) {
while (scanner.hasNext()) {
System.out.println(scanner.nextLine());
}
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
}
您不需要在此处关闭 scanner
,因为它会在 try
块执行完毕后自行关闭。如需更多参考,请访问此 blog
您不必关闭 您在 try 子句中定义的资源。但是,根据你的例子,你在 body 的 try:
中也有这个BufferedWriter bw = new BufferedWriter(new FileWriter(new File("output.txt"))))
并且您的代码没有关闭该资源。这是错误的(保持文件系统句柄打开很可能是真正的资源泄漏)。
换句话说:您可能想将 bw
添加到您的 try-with-resources 子句中,因此它与 S3Object object
的定义一致(请参阅