我在這裏有一個示例代碼。函數創建的FileInputStream是否會在代碼存在parentFunction的try/catch塊時自動關閉?函數中的Java AutoClosable行爲
或者是否需要在someOtherFunction()本身中顯式關閉?
private void parentFunction() {
try {
someOtherFunction();
} catch (Exception ex) {
// do something here
}
}
private void someOtherFunction() {
FileInputStream stream = new FileInputStream(currentFile.toFile());
// do something with the stream.
// return, without closing the stream here
return ;
}
你會需要在指定資源['嘗試,用-resources'](https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html)語句以便它自動關閉 –