23.07.2013 Views

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 2. Output Streams<br />

2.1 The OutputStream Class<br />

<strong>Java</strong> I/O<br />

The java.io.OutputStream class declares the three basic methods you need to write bytes<br />

of data onto a stream. It also has methods for closing and flushing streams.<br />

public abstract void write(int b) throws <strong>IO</strong>Exception<br />

public void write(byte[] data) throws <strong>IO</strong>Exception<br />

public void write(byte[] data, int offset, int length) throws <strong>IO</strong>Exception<br />

public void flush() throws <strong>IO</strong>Exception<br />

public void close() throws <strong>IO</strong>Exception<br />

OutputStream is an abstract class. Subclasses provide implementations of the abstract<br />

write(int b) method. They may also override the four nonabstract methods. For example,<br />

the FileOutputStream class overrides all five methods with native methods that know how<br />

to write bytes into files on the host platform. Although OutputStream is abstract, often you<br />

only need to know that the object you have is an OutputStream ; the more specific subclass<br />

of OutputStream is hidden from you. For example, the getOutputStream() method of<br />

java.net.URLConnection has the signature:<br />

public OutputStream getOutputStream() throws <strong>IO</strong>Exception<br />

Depending on the type of URL associated with this URLConnection object, the actual class of<br />

the output stream that's returned may be a sun.net.TelnetOutputStream , a<br />

sun.net.smtp.SmtpPrintStream , a sun.net.www.http.KeepAliveStream , or something<br />

else completely. All you know as a programmer, and all you need to know, is that the object<br />

returned is in fact some instance of OutputStream. That's why the detailed classes that handle<br />

particular kinds of connections are hidden inside the sun packages.<br />

Furthermore, even when working with subclasses whose types you know, you still need to be<br />

able to use the methods inherited from OutputStream. And since methods that are inherited<br />

are not included in the online documentation, it's important to remember that they're there. For<br />

example, the java.io.DataOutputStream class does not declare a close() method, but you<br />

can still call the one it inherits from its superclass.<br />

2.2 Writing Bytes to Output Streams<br />

The fundamental method of the OutputStream class is write():<br />

public abstract void write(int b) throws <strong>IO</strong>Exception<br />

This method writes a single unsigned byte of data whose value should be between and 255. If<br />

you pass a number larger than 255 or smaller than zero, it's reduced modulo 256 before being<br />

written.<br />

Example 2.1, AsciiChart, is a simple program that writes the printable ASCII characters (32<br />

to 126) on the console. The console interprets the numeric values as ASCII characters, not as<br />

numbers. This is a feature of the console, not of the OutputStream class or the specific<br />

subclass of which System.out is an instance. The write() method merely sends a particular<br />

34

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!