Previous | Next | Trail Map | Writing Java Programs | Input and Output Streams

Using DataInputStream and DataOutputStream

This page shows you how to use the DataInputStream and DataOutputStream classes from using an example, DataIOTest, that reads and writes tabular data (invoices for Java merchandise). The tabular data is formatted in columns where each column is separated from the next by tabs. The columns contain the sales price, the numer of units ordered, and a description of the item, like this.
19.99   12      Java T-shirt
9.99    8       Java Mug
DataOutputStream, like other filtered output streams, must be attached to some other OutputStream. In this case, its attached to a FileOutputStream set up to write to a file on the file system named invoice1.txt.
DataOutputStream dos = new DataOutputStream(
			   new FileOutputStream("invoice1.txt"));
Next, DataIOTest uses DataOutputStream's specialized writeXXX() methods to write the invoice data (contained within arrays in the program).
for (int i = 0; i < prices.length; i ++) {
Note that this code snippet closes the output stream when its finished. The close() method flushes the stream before closing it.

Next, DataIOTest opens a DataInputStream on the file just written:

DataInputStream dis = new DataInputStream(
			  new FileInputStream("invoice1.txt"));
DataInputStream, like other filtered input streams, must be attached to some other InputStream. In this case, its attached to a FileInputStream set up to read from a file on the file system named invoice1.txt. DataIOTest then just reads the data back in using DataInputStream's specialized readXXX() methods to read the input data into Java variables of the correct type.
while (!EOF) {
    try {
        price = dis.readDouble();
        dis.readChar();       // throws out the tab
        unit = dis.readInt();
        dis.readChar();       // throws out the tab
        desc = dis.readLine();
        System.out.println("You've ordered " + unit + " units of " + desc + " at " + price);
        total = total + unit * price;
    } catch (EOFException e) {
        EOF = true;
System.out.println("For a TOTAL of: " + total);
When all of the data has been read, DataIOText displays a statement summarizing the order and the total amount owed, and closes the stream.

Note the loop that DataIOTest uses to read the data from the DataInputStream. Normally, when reading you see loops like this:

while ((input = dis.readLine()) != null) {
    . . .
The readLine() method returns some value, null, that indicates that the end of the file has been reached. Many of the DataInputStream methods can't do this because any value that can used as an end-of-file indicator may also be a valid input value--what double value could readDouble() return to indicate end-of-file that wasn't a valid input value? So these methods throw an EOFException instead. When he DataIOTest program catches the EOFException it sets a boolean value, EOF, to true. The while loop continues to read until and end-of-file has been reached (EOF becomes true).

When you run the DataIOTest program you should see the following output:

You've ordered 12 units of Java T-shirt at 19.99
You've ordered 8 units of Java Mug at 9.99
You've ordered 13 units of Duke Juggling Dolls at 15.99
You've ordered 29 units of Java pin at 3.99
You've ordered 50 units of Java Key Chain at 4.99
For a TOTAL of: 892.88

See also


Previous | Next | Trail Map | Writing Java Programs | Input and Output Streams