Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

like image 988
Eng.Fouad Avatar asked Sep 17 '11 20:09

Eng.Fouad


People also ask

Why we need to close the stream objects always after usage?

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.

Why should streams be closed?

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.

Is it necessary to close stream in Java?

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.

Do you need to close Inputstreamreader?

Therefore, if you close the Reader, you don't need to also close the InputStream.


1 Answers

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.

like image 175
Darin Dimitrov Avatar answered Oct 18 '22 20:10

Darin Dimitrov