开发者社区> 问答> 正文

使用Java的java.nio.file.readAllLines时是否需要使用try-with-r

使用java.nio.file.readAllLines时,是否需要关闭资源?

我在网上的一些示例中看到他们没有这样做。

例如在这里https://www.programcreek.com/java-api-examples/?class=java.nio.file.Files&method=readAllLines 它们捕获IOException,但是它们不关闭任何资源。

任何帮助,将不胜感激,谢谢!

展开
收起
垚tutu 2019-12-26 18:16:03 1523 0
1 条回答
写回答
取消 提交回答
  • java源码中说了: Read all lines from a file. This method ensures that the file is * closed when all bytes have been read or an I/O error, or other runtime * exception, is thrown. Bytes from the file are decoded into characters * using the specified charset.

    其中,实现中使用了新的try{} 语句打开文件,所以会自动关闭文件;

    2020-03-20 19:38:54
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载