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.

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

However, this interface may be implemented by classes that aren't subclasses of<br />

InputStream.<br />

The ObjectOutput interface declares the following six methods, all of which<br />

ObjectOutputStream faithfully implements. Except for writeObject(), which has already<br />

been discussed in the context of object output streams, these methods should behave exactly<br />

as they do for all output streams:<br />

public abstract void writeObject(Object o) throws <strong>IO</strong>Exception<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 flush() throws <strong>IO</strong>Exception<br />

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

ObjectInput and ObjectOutput extend DataInput and DataOutput. Thus, as well as the<br />

methods declared outright, classes that implement ObjectInput must provide these additional<br />

methods as well:<br />

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

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

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

public abstract int skipBytes(int n) throws <strong>IO</strong>Exception<br />

public abstract boolean readBoolean() throws <strong>IO</strong>Exception<br />

public abstract byte readByte() throws <strong>IO</strong>Exception<br />

public abstract int readUnsignedByte() throws <strong>IO</strong>Exception<br />

public abstract short readShort() throws <strong>IO</strong>Exception<br />

public abstract int readUnsignedShort() throws <strong>IO</strong>Exception<br />

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

public abstract int readInt() throws <strong>IO</strong>Exception<br />

public abstract long readLong() throws <strong>IO</strong>Exception<br />

public abstract float readFloat() throws <strong>IO</strong>Exception<br />

public abstract double readDouble() throws <strong>IO</strong>Exception<br />

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

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

Classes that implement ObjectOutput must provide these additional methods:<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 />

As I noted back in Chapter 7, there's a bit of asymmetry between the DataInput and<br />

DataOutput interfaces. DataOutput declares the three write() methods you expect to find in<br />

248

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

Saved successfully!

Ooh no, something went wrong!