If
FileInputStream fileIS = new FileInputStream(filePathStr);
DataInputStream dataIS = new DataInputStream(fileIS);
does closing fileIS automatically close dataIS since dataIS is propagated fileIS or should dataIS also be closed separately?
Thanks
If given choice, you should close only the
DataInputStream. More generally, always close the outermost wrapper stream. The closing will propagate inwards and this is the only way to generally ensure correct behavior.However, if you close the underlying
FileInputStream, that will also be enough because theDataInputStreamdoesn’t by itself acquire any system resources.The most direct answer to your question: no, closing the underlying stream does not close the wrapper stream, but in practice that is irrelevant from the perspective of system resource leaks. Only the stream at the bottom is coupled to the actual system resource.