The
java.io.OutputStream
class declares the three basic methods you
need to write bytes of data onto a stream. It also has methods for
closing and flushing streams.
public abstract void write(int b) throws IOException public void write(byte[] data) throws IOException public void write(byte[] data, int offset, int length) throws IOException public void flush() throws IOException public void close() throws IOException
OutputStream
is an abstract class. Subclasses
provide implementations of the abstract write(int
b)
method. They may also override the four
nonabstract methods. For example, the
FileOutputStream
class overrides all five methods
with native methods that know how to write bytes into files on the
host platform. Although OutputStream
is abstract,
often you only need to know that the object you have is an
OutputStream
; the more specific subclass of
OutputStream
is hidden from you. For example, the
getOutputStream()
method of
java.net.URLConnection
has the signature:
public OutputStream getOutputStream() throws IOException
Depending on the type of URL associated with this
URLConnection
object, the actual class of the
output stream that’s returned may be a
sun.net.TelnetOutputStream
, a
sun.net.smtp.SmtpPrintStream
, a
sun.net.www.http.KeepAliveStream
, or something
else completely. All you know as a programmer, and all you need to
know, is that the object returned is in fact some instance of
OutputStream
. That’s why the detailed
classes that handle particular kinds of connections are hidden inside
the sun
packages.
Furthermore, even when working with subclasses whose types you know,
you still need to be able to use the methods inherited from
OutputStream
. And since methods that are inherited
are not included in the online documentation, it’s important to
remember that they’re there. For example, the
java.io.DataOutputStream
class does not declare a
close()
method, but you can still call the one it
inherits from its superclass.
18.225.95.107