The Data Stream Classes

The and 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 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 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.

Table 8-1. Formats used by DataInput and DataOutput


Written by

Read by



writeBoolean(boolean b)

readBoolean( )

One byte, 0 if false, 1 if true


writeByte(int b)

readByte( )

One byte, two's complement

byte array

write(byte[] data)


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


writeShort(int s)

readShort( )

Two bytes, two's complement, big-endian


writeChar(int c)

readChar( )

Two bytes, unsigned, big-endian


writeInt(int i)

readInt( )

Four bytes, two's complement, big-endian


writeLong(long l)

readLong( )

Eight bytes, two's complement, big-endian


writeFloat(float f)

readFloat( )

Four bytes, IEEE 754, big-endian


writeDouble(double d)

readDouble( )

Eight bytes, IEEE 754, big-endian

unsigned byte


readUnsignedByte( )

One unsigned byte

unsigned short


readUnsignedShort( )

Two bytes, big-endian, unsigned


writeBytes(String s)


The low-order byte of each char in the string from first to last


writeChars(String s)


Both bytes of each char in the string from first to last


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")

Basic I/O

Introducing I/O

Output Streams

Input Streams

Data Sources

File Streams

Network Streams

Filter Streams

Filter Streams

Print Streams

Data Streams

Streams in Memory

Compressing Streams

JAR Archives

Cryptographic Streams

Object Serialization

New I/O



Nonblocking I/O

The File System

Working with Files

File Dialogs and Choosers


Character Sets and Unicode

Readers and Writers

Formatted I/O with java.text


The Java Communications API


The J2ME Generic Connection Framework


Character Sets

show all menu

Java I/O
Java I/O
ISBN: 0596527500
EAN: 2147483647
Year: 2004
Pages: 244
Similar book on Amazon © 2008-2017.
If you may any questions please contact us: