This page contains an archived post to the Design Forum (formerly called the Flexible Java Forum) made prior to February 25, 2002.
If you wish to participate in discussions, please visit the new
Calling close() twice
Posted by Bill Venners on 14 May 1998, 1:07 PM
One of the things in the LogFile class of this article
that could potentially be redundant is that I call close()
on both FileOutputStream and PrintWriter.
One might expect that the close() on PrintWriter would in
turn call close() on the ObjectStream is wraps, but this
isn't clear from the documentation. (I looked in several
places.) The documentation for OutputStream's close() does
say it "releases any system resources associated with the
stream," which is what I was looking to do. So I call close()
twice just to be sure.
Seems like this happens from time to time, and it isn't usually
good enough to just try it and see if it works, because even
if it works on the environment I happen to be using, if it
isn't well specified, then there may be other Java environments
out there where it doesn't work. So I try to program
defensively (here, defending against vague documentation and
potentially against various ways the Java environment can be
implemented by different vendors.)