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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

PipedReader pr = new PipedReader();<br />

PipedWriter pw = new PipedWriter(pr);<br />

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

Or you can create them both unconnected, then use one or the other's connect() method to<br />

link them:<br />

public void connect(PipedReader sink) throws <strong>IO</strong>Exception<br />

public void connect(PipedWriter source) throws <strong>IO</strong>Exception<br />

PipedWriter's connect() method takes as an argument the PipedReader to connect to.<br />

PipedReader's connect() argument takes as an argument the PipedWriter to connect to:<br />

PipedReader pr = new PipedReader();<br />

PipedWriter pw = new PipedWriter();<br />

pr.connect(pw);<br />

or:<br />

PipedReader pr = new PipedReader();<br />

PipedWriter pw = new PipedWriter();<br />

pw.connect(pr);<br />

Neither a PipedWriter nor a PipedReader may be connected to more than one reader or<br />

writer. Attempts to do so throw <strong>IO</strong>Exceptions. Furthermore, once connected, a<br />

PipedWriter/PipedReader pair may not be disconnected. Otherwise, these classes have the<br />

usual read(), write(), flush(), close(), and ready() methods like all reader and writer<br />

classes. These are the remaining declared methods in PipedWriter:<br />

public void write(char[] text, 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 />

When characters are written on the PipedWriter, that text becomes available as input to be<br />

read by the connected PipedReader. If a PipedReader tries to read characters, but its<br />

connected PipedWriter hasn't yet provided it with any, the PipedReader blocks.<br />

These are the remaining declared methods in PipedReader:<br />

public int read(char[] text, int offset, int length) throws <strong>IO</strong>Exception<br />

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

Closing either a PipedReader or a PipedWriter also closes the reader or writer it's connected<br />

to.<br />

15.11 Filtered Readers and Writers<br />

The java.io.FilterReader and java.io.FilterWriter classes are abstract classes that<br />

read characters and filter them in some way before passing the text along. You can imagine a<br />

FilterReader that converts all characters to uppercase.<br />

public abstract class FilterReader extends Reader<br />

public abstract class FilterWriter extends Writer<br />

381

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

Saved successfully!

Ooh no, something went wrong!