why do we need to close streams in java manually? because at end of the program the garbage collector will close automatically.
Asked
Active
Viewed 20 times
0
-
1) The garbage collector doesn't run when a program exits. 2) In fact, it is the operating system that marks the streams as closed, but it won't (can't) flush any buffered output. 3) The *real* reason you need to close streams is that if you don't you are liable to run out of **file descriptors** while the application is still running, and when that happens any attempts to open files, sockets, pipes and so on will be refused by the OS; i.e. they will fail. – Stephen C Nov 11 '22 at 11:41