Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Closing chained I/O streams

729894Feb 28 2010 — edited Mar 12 2010
When chaining high-level I/O streams with low-level streams, without creating a reference to latter, are they getting closed automatically?
Let me clarify the situation.
That is a sample code from a book:
FileInputStream fis = new FileInputStream("file.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);
/*
* some code here
*/
dis.close();
bis.close();
fis.close();
As for me, I prefer cleaner solution, such as:
DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream("file.txt")));
But in this case, I can't close low level stream FileInputStream and BufferedInputStream, since I don't have any references to them.
Does the call to close() on BufferedInputStream object (bis.close()) automatically closes all lower-level streams?
Which snippet is usually considered a better approach to chaining streams?
Thank you.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 9 2010
Added on Feb 28 2010
6 comments
242 views