Why don't we close `System.out` Stream after using it?

巧了我就是萌 提交于 2019-11-26 23:28:02

问题


I just want to know, we usually close streams at the end, but why don't we close System.out PrintStream with System.out.close()?


回答1:


If you close it you will no longer be able to write to the console, so let's leave this task to the VM when the process terminates. You should only close streams that you own or have manually created. System.out is out of your control, so leave it to the creator to take care of.




回答2:


because we didn't open it the VM did and it's his job to close it unless otherwise documented

it's similar to the C++ adage of I don't own it, don't delete it.




回答3:


You can still flush() it, of course.



来源:https://stackoverflow.com/questions/7457723/why-dont-we-close-system-out-stream-after-using-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!