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.