| Author |
Question about ObjectOutputStream close() method
|
Balraj Momi
Ranch Hand
Joined: Jul 23, 2009
Posts: 45
|
|
I know that close() method inside streams is to release all the resources that were used for a operation.
Question is what exactly are the side effect if we don't close it?
Following code does something like this
Please don't think silly of me by seeing "ObjectOutputStream stream" as static, I took it just to keep this Stream object alive for next write Operation. If you notice, First method call writeData() used text.txt file to write a object and did not close the stream. Later second method opened another stream to write to same file and so does the read operation. As far resources are concerned, should not we get IOException or something in this case.
Please do let me know If my assumption is wrong.
Regards
Balraj
|
Regards
Balraj Kumar
SCJP 5 95%
SCWCD 82%
Preparing for SCBCD
|
 |
Sebastian Janisch
Ranch Hand
Joined: Feb 23, 2009
Posts: 1183
|
|
To your side-effects question.
If you do not close a stream to a resource or file, the underlying OS keeps a pointer to it that is used by the JVM.
You have probably experienced the Windows error message 'file could not be deleted because it is used by another process'.
This is one of the side effects.
|
JDBCSupport - An easy to use, light-weight JDBC framework -
|
 |
 |
|
|
subject: Question about ObjectOutputStream close() method
|
|
|