|
|
Reading and Writing |
DataInputStream and DataOutputStream
This page shows you how to use thejava.ioDataInputStreamand
DataOutputStreamclasses. 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 aFileOutputStreamthat's set up to write to a file namedinvoice1.txt.Next,DataOutputStream dos = new DataOutputStream( new FileOutputStream("invoice1.txt"));DataIOTestusesDataOutputStream's specializedwriteXXXmethods 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,
DataIOTestopens aDataInputStreamon the file just written:DataInputStream dis = new DataInputStream( new FileInputStream("invoice1.txt"));DataInputStreamalso must be attached to some otherInputStream; in this case, aFileInputStreamset up to read the file just written--invoice1.txt.DataIOTestthen just reads the data back in usingDataInputStream's specializedreadXXXmethods.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();DataIOTestdisplays a statement summarizing the order and the total amount owed, and closes the stream.Note the loop that
DataIOTestuses to read the data from theDataInputStream. Normally, when reading you see loops like this:Thewhile ((input = dis.readLine()) != null) { . . . }readLinemethod returns a value, null, that indicates that the end of the file has been reached. Many of theDataInputStreamreadXXXmethods 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'sreadXXXmethods throw anEOFExceptioninstead. When theEOFExceptionoccurs thewhile (true)terminates.When you run the
DataIOTestprogram 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
|
|
Reading and Writing |