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.

7.2.2 The Char Format<br />

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

Unicode characters are two bytes long and are interpreted as an unsigned number between and<br />

65,535. This means they have an "endianness" problem too. The Unicode standard<br />

specifically does not require a particular endianness of text written in Unicode; both big- and<br />

little-endian encodings are allowed. In my opinion, this is a failing of the specification. The<br />

Unicode standard does suggest that character 65,279 (0xFEFF in hex) be placed at the<br />

beginning of each file of Unicode text. Thus, by reading the first character, you can determine<br />

the endianness of the file and take appropriate action. For example, if you're reading a<br />

Unicode file containing little-endian data using big-endian methods, the first character will<br />

appear as 0xFFFE (65,534), signaling that something is wrong. <strong>Java</strong>'s data stream classes<br />

always write and read Unicode text in a big-endian fashion.<br />

7.2.3 Writing Integers<br />

The DataOutputStream class has the usual three write() methods you'll find in any output<br />

stream class:<br />

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

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

public synchronized void write(byte[] data, int offset, int length)<br />

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

These behave exactly as they do in the superclass, so I won't discuss them further here.<br />

The DataOutputStream class also declares the following void methods that write signed<br />

integer types onto its underlying output stream:<br />

public final void writeByte(int b) throws <strong>IO</strong>Exception<br />

public final void writeShort(int s) throws <strong>IO</strong>Exception<br />

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

public final void writeLong(long l) throws <strong>IO</strong>Exception<br />

Because <strong>Java</strong> doesn't fully support the byte or short types, the writeByte() and<br />

writeShort() methods each take an int as an argument. The excess bytes in the int are<br />

ignored before the byte or short is written. Thus writeByte() only writes the low-order<br />

byte of its argument. The writeShort() method only writes the low-order two bytes of its<br />

argument, higher-order byte first; that is, big-endian order. The writeInt() and<br />

writeLong() methods write all the bytes of their arguments in big-endian order. These<br />

methods can throw <strong>IO</strong>Exceptions if the underlying stream throws an <strong>IO</strong>Exception.<br />

Example 7.1 fills a file called 1000.dat with the integers between 1 and 1000. This filename is<br />

used to construct a FileOutputStream. This stream is then chained to a DataOutputStream<br />

whose writeInt() method writes the data into the file.<br />

Example 7.1. One Thousand ints<br />

import java.io.*;<br />

public class File1000 {<br />

100

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

Saved successfully!

Ooh no, something went wrong!