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 java.io.DataOutput interface declares 14 methods, mostly complementary to those in<br />

DataInput:<br />

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

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

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

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

public abstract void writeBoolean(boolean v) throws <strong>IO</strong>Exception<br />

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

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

public abstract void writeChar(int c) throws <strong>IO</strong>Exception<br />

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

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

public abstract void writeFloat(float f) throws <strong>IO</strong>Exception<br />

public abstract void writeDouble(double d) throws <strong>IO</strong>Exception<br />

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

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

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

The writeBytes() and writeChars() methods are not matched by readBytes() and<br />

readChars() methods in DataInput. The format used only writes the actual bytes and chars.<br />

It does not write information about the length of the string passed as an argument to<br />

writeBytes() and writeChars(), so the bytes and chars themselves cannot be easily<br />

reassembled into a string. It is also unclear why DataOutput declares the three common<br />

write() methods, but DataInput does not declare the three common read() methods.<br />

However, the two readFully() methods are actually better matches for write(), since,<br />

unlike read(), they will either fill the array or throw an exception.<br />

Although DataInput and DataOutput say nothing about the formats in which data is read<br />

and written, any class that implements this interface must adhere to an implicit contract,<br />

summarized in Table 7.1.<br />

Table 7.1. Formats Used by DataInput and DataOutput<br />

Type Written by Read by Format<br />

boolean writeBoolean(boolean<br />

b)<br />

readBoolean()<br />

One byte, 0 if false, 1 if<br />

true<br />

byte writeByte(int b) readByte() One byte, two's complement<br />

byte<br />

array<br />

write(byte[]<br />

data)write(byte[]<br />

data, int offset, int<br />

length)<br />

readFully(byte[]<br />

data)readFully(byte[]<br />

data, int offset, int<br />

length)<br />

short writeShort(int s) readShort()<br />

char writeChar(int c) readChar()<br />

int writeInt(int i) readInt()<br />

long writeLong(long l) readLong()<br />

float writeFloat(float f) readFloat()<br />

double writeDouble(double d) readDouble()<br />

The bytes in the order they<br />

appear in the array or subarray<br />

Two bytes, two's complement,<br />

big- endian<br />

Two bytes, unsigned, bigendian<br />

Four bytes, two's complement,<br />

big-endian<br />

Eight bytes, two's<br />

complement, big-endian<br />

Four bytes, IEEE 754, bigendian<br />

Eight bytes, IEEE 754, bigendian<br />

97

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

Saved successfully!

Ooh no, something went wrong!