![]() ![]() ![]() |
Start of Tutorial > Start of Trail > Start of Lesson | Search |
[PENDING: consider using reader's re-written version of this program.]This page shows you how to use the
java.io
DataInputStream
and
DataOutputStream
classes. It features 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 number of units ordered, and a description of the item, like this:
19.99 12 Java T-shirt 9.99 8 Java MugDataOutputStream
, like other filtered output streams, must be attached to some otherOutputStream
. In this case, it's attached to aFileOutputStream
that's set up to write to a file namedinvoice1.txt
.Next,DataOutputStream dos = new DataOutputStream( new FileOutputStream("invoice1.txt"));DataIOTest
usesDataOutputStream
's specializedwriteXXX
methods to write the invoice data (contained within arrays in the program) according to the type of data being written:Note that this code snippet closes the output stream when it's finished.for (int i = 0; i < prices.length; i ++) { dos.writeDouble(prices[i]); dos.writeChar('\t'); dos.writeInt(units[i]); dos.writeChar('\t'); dos.writeChars(descs[i]); dos.writeChar('\n'); } dos.close();Next,
DataIOTest
opens aDataInputStream
on the file just written:DataInputStream dis = new DataInputStream( new FileInputStream("invoice1.txt"));DataInputStream
also must be attached to some otherInputStream
; in this case, aFileInputStream
set up to read the file just written--invoice1.txt
.DataIOTest
then just reads the data back in usingDataInputStream
's specializedreadXXX
methods.When all of the data has been read,try { while (true) { 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) { } System.out.println("For a TOTAL of: $" + total); dis.close();DataIOTest
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 theDataInputStream
. Normally, when reading you see loops like this:Thewhile ((input = dis.readLine()) != null) { . . . }readLine
method returns a value, null, that indicates that the end of the file has been reached. Many of theDataInputStream
readXXX
methods can't do this because any value that could be returned to indicate end-of-file may also be a legitimate value read from the stream. For example, suppose that you wanted to use -1 to indicate end-of-file? Well, you can't because -1 is a legitimate value that can be read from the input stream usingreadDouble
,readInt
, or one of the other read methods that reads numbers. SoDataInputStream
'sreadXXX
methods throw anEOFException
instead. When theEOFException
occurs thewhile (true)
terminates.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
![]() ![]() ![]() |
Start of Tutorial > Start of Trail > Start of Lesson | Search |