#Conceptual question about streams
1 messages · Page 1 of 1 (latest)
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
When I have a line such as new ObjectOutputStream(new ZipOutputStream(new FileOutputStream("some file")););.
I’m basically creating, as far as I understand it, a stream towards some certain file, which can already exist or it will be created. (With new FileOutputStream("some file")) Afterwards, and correct me if I’m wrong, I’m applying a certain number of filters on this stream towards the file, by having access to certain methods which let me use those filters, for example, by having the capacity to write/translate objects in a stream of byte ( With new ObjectOutputStream) and this stream of byte, I assume, is compressed to a ZIP format by using (new ZipOutputStream) before being sent off officially towards the file. (Though new FileOutputStream("some file"));) I'm basically preparing the "stream" to a certain format before officially sending it off, instead of directly dealing with the bytes.
Why is it, in this case, that when I need to close the stream, if my understanding is correct (it might not be), I always have to close the last “filter” (ObjectOutputStream in this case) which I use and not the actual stream towards the file (FileOutputStream) through which we send our data?
Hopefully my question makes sense
Well you don't strictly need to close it I think, but you do need to flush the stream (which is what closing does as well). You're basically telling the filters as you call them to write whatever is buffered to disk
Just checked, I am incorrect regarding ZipOutputStream, that does need to be closed
Because it's a signal to the deflate stream that it has all the data, which means it'll write the last chunk of compressed data
Rather than waiting for more to compress it with
How would you explain reading from a file? Should we not close the inputstream towards the file first before closing the outermost method which deserializes everything?
Intuitively, it can make sense to close the outermost method, because we want to flush eveything towards the file while writing, but when we read should we not close the input towards the file first, so starting from innermost, and towards outermost?
Is this what is actually happening?