Next: The DataOutputStream class. Up: Binary files. Previous: Binary vs text files.


The DataInputStream class

Textbook: Section 14.5

The DataInputStream class is layed on top of the FileInputStream class (actually, it's layered on top of the InputStream class, of which FileInputStream is a subclass).

DataInputStream(FileInputStream in)
Creates a DataInputStream that uses in whenever it wants to read more bytes.

It provides a variety of methods for reading data from the file.
int read(byte[] b)
Attempts to fill b with bytes, returning the number of bytes read - this is the same thing as what FileInputStream provided.

int readInt()
Reads 4 bytes from the file and returns the int that these 4 bytes represent.

double readDouble()
Reads 8 bytes from the file and returns the double that these 8 bytes represent.

String readUTF()
Reads and returns a string from the file.

When reading a number, the computer reads the binary representation from the file, with the most significant byte first. For example, if the file contained the following four bytes (representing each byte as two hexadecimal digits)
00 00 02 04
the DataInputStream would read this as the binary number 1000000100(2), which is 1028(10).

The UTF format is a standard for representing strings in a file, engineered to make it easy to recover a string from a file, regardless of what computer you're using. It begins with two bytes saying in binary how many bytes long the remainder of the string representation is, followed by the characters of the string represented in UTF-8 format (which basically represents ASCII characters as is, with non-ASCII Unicode characters handled in a more complex way). For example, the string ``CAB'' would be represented by the following five bytes (represented here in hexadecimal).

00 03 43 41 42


Next: The DataOutputStream class. Up: Binary files. Previous: Binary vs text files.