The Data Stream Classes
The java.io.DataInputStream and java.io.DataOutputStream classes are subclasses of FilterInputStream and FilterOutputStream, respectively.
public class DataInputStream extends FilterInputStream implements DataInput public class DataOutputStream extends FilterOutputStream implements DataOutput
They have all the usual methods you expect in input and output stream classes, such as read( ), write( ), flush( ), available( ), skip( ), close( ), markSupported( ), and reset( ). (Data input streams support marking if, and only if, their underlying input stream supports marking.) However, the real purpose of DataInputStream and DataOutputStream is not to read and write raw bytes using the standard input and output stream methods. It's to read and interpret multibyte data like ints, floats, doubles, and chars.
8.1.1. The DataInput and DataOutput Interfaces
The java.io.DataInput interface declares 15 methods that read various kinds of data:
public boolean readBoolean( ) throws IOException public byte readByte( ) throws IOException public int readUnsignedByte( ) throws IOException public short readShort( ) throws IOException public int readUnsignedShort( ) throws IOException public char readChar( ) throws IOException public int readInt( ) throws IOException public long readLong( ) throws IOException public float readFloat( ) throws IOException public double readDouble( ) throws IOException public String readLine( ) throws IOException public String readUTF( ) throws IOException public void readFully(byte[] data) throws IOException public void readFully(byte[] data, int offset, int length) throws IOException public int skipBytes(int n) throws IOException
These methods are all available from the DataInputStream class and any other class that implements DataInput. (In the core Java API, this includes DataInputStream, ObjectInputStream, RandomAccessFile, and several stream classes in the Java Image I/O API.)
Likewise, the java.io.DataOutput interface declares 14 methods, mostly complementary to those in DataInput:
public void write(int b) throws IOException public void write(byte[] data) throws IOException public void write(byte[] data, int offset, int length) throws IOException public void writeBoolean(boolean v) throws IOException public void writeByte(int b) throws IOException public void writeShort(int s) throws IOException public void writeChar(int c) throws IOException public void writeInt(int i) throws IOException public void writeLong(long l) throws IOException public void writeFloat(float f) throws IOException public void writeDouble(double d) throws IOException public void writeBytes(String s) throws IOException public void writeChars(String s) throws IOException public void writeUTF(String s) throws IOException
The writeBytes( ) and writeChars( ) methods are not matched by readBytes( ) and readChars( ) methods in DataInput. writeBytes( ) and writeChars( ) only write the actual bytes and chars. They do not write the length of the string passed as an argument to writeBytes( ) and writeChars( ), so the bytes and chars cannot easily be reassembled into a string.
Any class that implements these interfaces must use the binary data format summarized in Table 8-1.
Type |
Written by |
Read by |
Format |
---|---|---|---|
boolean |
writeBoolean(boolean b) |
readBoolean( ) |
One byte, 0 if false, 1 if true |
byte |
writeByte(int b) |
readByte( ) |
One byte, two's complement |
byte array |
write(byte[] data) write(byte[] data, int offset, int length) |
readFully(byte[] data) readFully(byte[] data, int offset, int length) |
The bytes in the order they appear in the array or subarray |
short |
writeShort(int s) |
readShort( ) |
Two bytes, two's complement, big-endian |
char |
writeChar(int c) |
readChar( ) |
Two bytes, unsigned, big-endian |
int |
writeInt(int i) |
readInt( ) |
Four bytes, two's complement, big-endian |
long |
writeLong(long l) |
readLong( ) |
Eight bytes, two's complement, big-endian |
float |
writeFloat(float f) |
readFloat( ) |
Four bytes, IEEE 754, big-endian |
double |
writeDouble(double d) |
readDouble( ) |
Eight bytes, IEEE 754, big-endian |
unsigned byte |
N/A |
readUnsignedByte( ) |
One unsigned byte |
unsigned short |
N/A |
readUnsignedShort( ) |
Two bytes, big-endian, unsigned |
String |
writeBytes(String s) |
N/A |
The low-order byte of each char in the string from first to last |
String |
writeChars(String s) |
N/A |
Both bytes of each char in the string from first to last |
String |
writeUTF(String s) |
readUTF( ) |
A signed short giving the number of bytes in the encoded string, followed by a modified UTF-8 encoding of the string |
8.1.2. Constructors
The DataInputStream and DataOutputStream classes have exactly the constructors you would expect:
public DataInputStream(InputStream in) public DataOutputStream(OutputStream out)
These constructors chain the data streams to the underlying streams passed as arguments. For example, to read formatted data from a file called data.txt and write formatted data to output.dat, you would create the two streams dis and dos:
DataInputStream dis = new DataInputStream(new FileInputStream("data.txt")); DataOutputStream dos = new DataOutputStream( new FileOutputStream("output.dat") );