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 />

The writeChar() method writes a single, two-byte Unicode character. This method does not<br />

use UTF-8 encoding. It simply writes the two bytes of the character in big-endian order.<br />

writeChars() writes each character in the String argument to the underlying output stream<br />

as a two-byte, Unicode character; it also does not use UTF-8 encoding. And the<br />

writeBytes() method writes the low-order byte of each character in the String argument to<br />

the underlying output stream. Any information in the high-order byte is lost. In other words, it<br />

assumes the string is given in ISO-Latin-1 and contains only characters whose value is<br />

between and 255.<br />

The writeUTF() method, however, retains the information in the high-order byte as well as<br />

the length of the string. First it writes the number of characters in the string onto the<br />

underlying output stream as a two-byte unsigned int between and 65,535. Next it encodes the<br />

string in UTF-8 and writes the bytes of the encoded string to the underlying output stream.<br />

This allows a data input stream reading those bytes to completely reconstruct the string.<br />

However, strings longer than 65,535 characters are a problem. If you pass a string longer than<br />

65,535 characters to writeUTF(), writeUTF() throws a java.io.UTFDataFormat-<br />

Exception , a subclass of <strong>IO</strong>Exception , and doesn't write any of the data. Although 65,535<br />

characters seems like a large limit, I can imagine reaching it if you're writing a word<br />

processor, text editor, or web browser and trying to save your data by writing it out as a single<br />

string. If you are writing a program that needs to deal with large blocks of text, you should<br />

write your own file format and save routine rather than relying on writeUTF().<br />

7.6.3 Reading Text<br />

The DataInputStream class has three methods to read text data. These are:<br />

public final char readChar() throws <strong>IO</strong>Exception<br />

public final String readUTF() throws <strong>IO</strong>Exception<br />

public static final String readUTF(DataInput in) throws <strong>IO</strong>Exception<br />

The readChar() method reads two bytes from the underlying input stream, interprets them as<br />

a big-endian Unicode character, and returns that character as a <strong>Java</strong> char. An <strong>IO</strong>Exception is<br />

thrown if the underlying input stream's read() method throws an <strong>IO</strong>Exception. An<br />

EOFException may be thrown if you're within one byte of the end of the stream and therefore<br />

a complete char can't be read.<br />

The noargs readUTF() method reads and returns a string that was written in <strong>Java</strong>'s pseudo-<br />

UTF-8 encoding with a two-byte, unsigned length prefix (in other words, a string written by<br />

writeUTF() in DataOutputStream). An EOFException is thrown if the end of the stream is<br />

encountered or the stream runs out of data before providing the promised number of<br />

characters. A UTFDataFormatException is thrown if the bytes read do not match the UTF<br />

encoding specification; for example, if four bytes in a row begin with the bit sequence 10. An<br />

<strong>IO</strong>Exception is also thrown if the underlying stream throws an <strong>IO</strong>Exception.<br />

7.6.4 The Deprecated readLine( ) Method<br />

The DataInputStream class also has a widely used but deprecated readLine() method:<br />

public final String readLine() throws <strong>IO</strong>Exception<br />

108

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

Saved successfully!

Ooh no, something went wrong!