Why don't we close `System.out` Stream after u

2019-01-15 12:19发布

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()?

3条回答
我只想做你的唯一
2楼-- · 2019-01-15 12:43

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.

查看更多
The star\"
3楼-- · 2019-01-15 12:58

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.

查看更多
你好瞎i
4楼-- · 2019-01-15 13:02

You can still flush() it, of course.

查看更多
登录 后发表回答