Input and Output Streams |
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:DataOutputStream, like other filtered output streams, must be attached to some other OutputStream. In this case, it's attached to a FileOutputStream that's set up to write to a file named19.99 12 Java T-shirt 9.99 8 Java Muginvoice1.txt
.Next, DataIOTest uses DataOutputStream's specializedDataOutputStream dos = new DataOutputStream( new FileOutputStream("invoice1.txt"));writeXXX()
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 a DataInputStream on the file just written:
DataInputStream, also must be attached to some other InputStream. In this case, it's attached to a FileInputStream set up to read the file just written--DataInputStream dis = new DataInputStream( new FileInputStream("invoice1.txt"));invoice1.txt
. DataIOTest then just reads the data back in using DataInputStream's specializedreadXXX()
methods.When all of the data has been read, DataIOTest displays a statement summarizing the order and the total amount owed, and closes the stream.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();Note the loop that DataIOTest uses to read the data from the DataInputStream. 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 the DataInputStreamreadXXX()
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. So DataInputStream'sreadXXX()
methods throw an EOFException instead. When the EOFException 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.88See also
java.io.DataInputStream
java.io.DataOutputStream
Input and Output Streams |