BufferedReader 正确使用
BufferedReader proper usage
我使用以下样式通过 BufferedReader 读取文件
try (BufferedReader br = new BufferedReader(new FileReader("my_file"))) {
...
br.close();
} catch( IOException e ) {
System.out.println( e.getMessage() );
}
我想知道的事情:
1- close()
是否在正确的位置?
2- 我应该为 `close() 添加另一个 try..catch
吗?
3- 由于我使用 new
作为 br
,是否足以调用 close()
或我必须为 GC 编写 br = null
?
4- FileReader
已经被 new
编辑了,我应该销毁它吗?
1-2- close 最好与finally 或resource 块一起使用。否则,如果之前发生异常,则不会调用关闭。
3-调用close()即可。将对象设置为 null 不会删除引用。当不再有对对象的引用时,GC 将销毁对象。所以不要手动销毁你的对象。
您在代码中使用了 try-with-resources
语句。在此示例中,try-with-resources
语句中声明的资源是 BufferedReader
。声明语句紧跟在 try
关键字之后出现在括号内。 Java SE 7 and later
中的 class BufferedReader
实现了接口 java.lang.AutoCloseable
。因为BufferedReader
实例是在try-with-resource
语句中声明的,所以无论try
语句是否正常完成,它都会关闭 或 突然 。您可以从 documentation 了解更多关于此声明的信息。所以,这里是修改后的版本:
try (BufferedReader br = new BufferedReader(new FileReader("my_file"))) {
// your logic
} catch (IOException e) {
System.out.println(e.getMessage());
}
您正在使用 try-with-resources 语句。您不需要在 Reader 上明确调用关闭。事实上文档状态:
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.
此外,您的文件Reader 由 BufferedReader 装饰,关闭 BufferedReader 应该关闭 FileReader.
- 由于您使用了 try-with-resource 语句,因此您无需明确关闭流。在任何情况下它都会自动关闭。如果您没有使用 try-with-resource,那么
close()
就会放在错误的位置。为确保您的资源始终关闭,您需要在 finally
块 中调用 close()
- 如果你在finally块中调用close(),你也需要捕获它抛出的checked Exception。如果你使用 try-with-resource(就像你所做的那样),你很好
- 你所有的变量都只存在于 try 块的范围内,所以你是安全的
- BufferedReader 关闭修饰的 Reader,因此您不必显式关闭它
我使用以下样式通过 BufferedReader 读取文件
try (BufferedReader br = new BufferedReader(new FileReader("my_file"))) {
...
br.close();
} catch( IOException e ) {
System.out.println( e.getMessage() );
}
我想知道的事情:
1- close()
是否在正确的位置?
2- 我应该为 `close() 添加另一个 try..catch
吗?
3- 由于我使用 new
作为 br
,是否足以调用 close()
或我必须为 GC 编写 br = null
?
4- FileReader
已经被 new
编辑了,我应该销毁它吗?
1-2- close 最好与finally 或resource 块一起使用。否则,如果之前发生异常,则不会调用关闭。
3-调用close()即可。将对象设置为 null 不会删除引用。当不再有对对象的引用时,GC 将销毁对象。所以不要手动销毁你的对象。
您在代码中使用了 try-with-resources
语句。在此示例中,try-with-resources
语句中声明的资源是 BufferedReader
。声明语句紧跟在 try
关键字之后出现在括号内。 Java SE 7 and later
中的 class BufferedReader
实现了接口 java.lang.AutoCloseable
。因为BufferedReader
实例是在try-with-resource
语句中声明的,所以无论try
语句是否正常完成,它都会关闭 或 突然 。您可以从 documentation 了解更多关于此声明的信息。所以,这里是修改后的版本:
try (BufferedReader br = new BufferedReader(new FileReader("my_file"))) {
// your logic
} catch (IOException e) {
System.out.println(e.getMessage());
}
您正在使用 try-with-resources 语句。您不需要在 Reader 上明确调用关闭。事实上文档状态:
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.
此外,您的文件Reader 由 BufferedReader 装饰,关闭 BufferedReader 应该关闭 FileReader.
- 由于您使用了 try-with-resource 语句,因此您无需明确关闭流。在任何情况下它都会自动关闭。如果您没有使用 try-with-resource,那么
close()
就会放在错误的位置。为确保您的资源始终关闭,您需要在finally
块 中调用 close()
- 如果你在finally块中调用close(),你也需要捕获它抛出的checked Exception。如果你使用 try-with-resource(就像你所做的那样),你很好
- 你所有的变量都只存在于 try 块的范围内,所以你是安全的
- BufferedReader 关闭修饰的 Reader,因此您不必显式关闭它