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.

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

For the most part, these classes have methods that are extremely similar to the equivalent<br />

stream classes. Often the only difference is that a byte in the signature of a stream method is<br />

replaced by a char in the signature of the matching reader or writer method. For example, the<br />

java.io.OutputStream class declares these three write() methods:<br />

public abstract void write(int i) 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 />

The java.io.Writer class, therefore, declares these three write() methods:<br />

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

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

public abstract void write(char[] data, int offset, int length) throws<br />

<strong>IO</strong>Exception<br />

As you can see, the six signatures are identical except that in the latter two methods the byte<br />

array data has changed to a char array. There's also a less obvious difference not reflected in<br />

the signature. While the int passed to the OutputStream write() method is reduced modulo<br />

256 before being output, the int passed to the Writer write() method is reduced modulo<br />

65,536. This reflects the different ranges of chars and bytes.<br />

java.io.Writer also has two more write() methods that take their data from a string:<br />

public void write(String s) throws <strong>IO</strong>Exception<br />

public void write(String s, int offset, int length) throws <strong>IO</strong>Exception<br />

Because streams don't know how to deal with character-based data, there are no<br />

corresponding methods in the java.io.OutputStream class.<br />

1.5 The Ubiquitous <strong>IO</strong>Exception<br />

As computer operations go, input and output are unreliable. They are subject to problems<br />

completely outside the programmer's control. Disks can develop bad sectors while a file is<br />

being read; construction workers drop backhoes through the cables that connect your WAN;<br />

users unexpectedly cancel their input; telephone repair crews shut off your modem line while<br />

trying to repair someone else's. (This last one actually happened to me while writing this<br />

chapter. My modem kept dropping the connection and then not getting a dial tone; I had to<br />

hunt down the telephone "repairman" in my building's basement and explain to him that he<br />

was working on the wrong line.)<br />

Because of these potential problems and many more, almost every method that performs input<br />

or output is declared to throw <strong>IO</strong>Exception. <strong>IO</strong>Exception is a checked exception, so you<br />

must either declare that your methods throw it or enclose the call that can throw it in a<br />

try/catch block. The only real exceptions to this rule are the PrintStream and PrintWriter<br />

classes. Because it would be inconvenient to wrap a try/catch block around each call to<br />

System.out.println(), Sun decided to have PrintStream (and later PrintWriter) catch<br />

and eat any exceptions thrown inside a print() or println() method. If you do want to<br />

check for exceptions inside a print() or println() method, you can call checkError():<br />

public boolean checkError()<br />

25

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

Saved successfully!

Ooh no, something went wrong!