closing writers and outputstreams

Discussion in 'Java' started by Andy Fish, Nov 13, 2003.

  1. Andy Fish

    Andy Fish Guest

    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
    Andy Fish, Nov 13, 2003
    #1
    1. Advertising

  2. "Andy Fish" <> writes:
    > 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
    Thomas Weidenfeller, Nov 13, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Yurai Núñez Rodríguez

    Problems using ASPNET OutputStreams

    Yurai Núñez Rodríguez, May 2, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    299
    Yurai Núñez Rodríguez
    May 2, 2004
  2. Bruce Lee
    Replies:
    1
    Views:
    358
    Steve Horsley
    Dec 7, 2004
  3. Philipp
    Replies:
    15
    Views:
    695
    Tom Anderson
    May 17, 2008
  4. Knute Johnson

    Re: which OutputStreams are buffered?

    Knute Johnson, May 16, 2008, in forum: Java
    Replies:
    1
    Views:
    334
    Knute Johnson
    May 16, 2008
  5. Tom Anderson

    Re: which OutputStreams are buffered?

    Tom Anderson, May 16, 2008, in forum: Java
    Replies:
    9
    Views:
    448
    Tom Anderson
    May 21, 2008
Loading...

Share This Page