在所有案例中每个人都可以像这样找代码:DataInputStream类谁关闭流,如果IOException异常
DataInputStream inputStream = null;
try {
inputStream = new DataInputStream(new FileInputStream("file.data"));
int i = inputStream.readInt();
inputStream.close();
} catch (FileNotFoundException e) {
//print message File not found
} catch (IOException e) { e.printStackTrace() }
此代码时遇到FileNotFound
例外,inputStream
没有打开,所以它并不需要关闭。
但是为什么当IOException
在那个catch块我看不到inputStream.close()
。当输入数据异常抛出时,这个操作会自动执行吗?因为如果程序有输入问题,这意味着流已经打开。
你是什么意思在Java7中引入“try-catch-with-resources”?在你的代码部分我没有看到inputStream.close()运算符。你的意思是让Java关闭? –
@LesyaMakhova我建议你阅读这里的建设:http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html – Andremoniy
非常感谢。这是最好的解决方案! –