Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
This page shows you how to use thejava.io
DataInputStream
andDataOutputStream
classes. It features an example,DataIODemo
, that reads and writes tabular data (invoices for merchandise). The tabular data is formatted in columns separated by tabs. The columns contain the sales price, the number of units ordered, and a description of the item. Conceptually, the data looks like this, although it is read and written in binary form and is non-ASCII:19.99 12 Java T-shirt 9.99 8 Java MugDataOutputStream
, like other filtered output streams, must be attached to anotherOutputStream
. In this case, it's attached to aFileOutputStream
that is set up to write to a file namedinvoice1.txt
:Next,DataOutputStream out = new DataOutputStream( new FileOutputStream("invoice1.txt"));DataIODemo
usesDataOutputStream
's specializedwrite
methods to write the invoice data contained within arrays in the program according to the type of data being written:Next,for (int i = 0; i < prices.length; i ++) { out.writeDouble(prices[i]); out.writeChar('\t'); out.writeInt(units[i]); out.writeChar('\t'); out.writeChars(descs[i]); out.writeChar('\n'); } out.close();DataIODemo
opens aDataInputStream
on the file just written:DataInputStream in = new DataInputStream( new FileInputStream("invoice1.txt"));DataInputStream
also must be attached to anotherInputStream
; in this case, aFileInputStream
set up to read the file just written,invoice1.txt
. ThenDataIODemo
just reads the data back in usingDataInputStream
's specialized read methods.When all of the data has been read,try { while (true) { price = in.readDouble(); in.readChar(); //throws out the tab unit = in.readInt(); in.readChar(); //throws out the tab char chr; desc = new StringBuffer(20); char lineSep = System.getProperty("line.separator").charAt(0); while ((chr = in.readChar() != lineSep) { desc.append(chr); } 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); in.close();DataIODemo
displays a statement summarizing the order and the total amount owed and then closes the stream.Note the loop that
DataIODemo
uses to read the data from theDataInputStream
. Normally, when data is read, you see loops like this:Thewhile ((input = in.read()) != null) { . . . }read
method returns a value,null
, which indicates that the end of the file has been reached. Many of theDataInputStream
read methods can't do this, because any value that could be returned to indicate the end of file may also be a legitimate value read from the stream. For example, suppose that you want 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 methods that reads numbers. SoDataInputStream
s read methods throw anEOFException
instead. When theEOFException
occurs, thewhile (true)
terminates.When you run the
DataIODemo
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.8800000000001
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2002 Sun Microsystems, Inc. All rights reserved.