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()
?
You should always close a stream in order to free open resources on your OS. Opening a stream always returns an identifier which you can use to close it wherever you are in your code (as long as the identifier is valid), whether their from another method or class.
For programmers who have never done system programming using C or C++, this habit is more visible. To release the file descriptor held by this class as its limited resource and used in both socket connection and file handling, it is essential to close streams.
It is generally not necessary to close streams at all. You only need to close streams that use IO resources. From the Stream documentation: Streams have a BaseStream.
Therefore, if you close the Reader, you don't need to also close the InputStream.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With