closing writers and outputstreams

A

Andy Fish

Hi,

If I have an outputStream with a printWriter, is it necessary to close both?
If so, does it matter in which order? If I only need to close one, does it
matter which one?

How about other cases like an OutputStream with a BufferedOutputStream
attached to it?

Andy
 
T

Thomas Weidenfeller

Andy Fish said:
If I have an outputStream with a printWriter, is it necessary to close both?

No, if you close the outer one, the inner one (underlying, encapsulated
one) is closed, too (unless someone messed up the implementation of the
outer one, of course).
If so, does it matter in which order? If I only need to close one, does it
matter which one?

Close the outer one, and only the outer one. The inner one doesn't know
that there is an outer one.
How about other cases like an OutputStream with a BufferedOutputStream
attached to it?

The same. Those streams/readers/writers that can encapsulate another
stream/reader/writer know how to close these encapsulates ones.

See for example the close() implementation in FilterOutputStream, which
is among others the base clase for BufferedOutputStream.

/Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top