【发布时间】:2017-09-07 13:48:51
【问题描述】:
我在 java 要做的事:
try(InputStream inputStream = new FileInputStream("/home/user/123.txt")) {
byte[] bytes = new byte[inputStream.available()];
inputStream.read(bytes);
System.out.println(new String(bytes));
} catch (IOException e) {
e.printStackTrace();
}
但是kotlin 不知道try-with-resources!所以我的代码是
try {
val input = FileInputStream("/home/user/123.txt")
} finally {
// but finally scope doesn't see the scope of try!
}
有没有简单的方法来关闭流?而且我不仅仅谈论文件。有没有办法轻松关闭任何stream?
【问题讨论】: