![]() ![]() ![]() ![]() |
Input and Output Streams |
This page shows you how to use the DataInputStream and DataOutputStream classes from java.io 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.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 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).Note that this code snippet closes the output stream when its finished. Thefor (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();close()
method flushes the stream before closing it.Next, DataIOTest opens a DataInputStream on the file just written:
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 namedDataInputStream dis = new DataInputStream( new FileInputStream("invoice1.txt"));invoice1.txt
. DataIOTest then just reads the data back in using DataInputStream's specializedreadXXX()
methods to read the input data into Java variables of the correct type.When all of the data has been read, DataIOText displays a statement summarizing the order and the total amount owed, and closes the stream.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); 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 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 couldreadDouble()
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. Thewhile
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.88See also
java.lang.DataInputStream
java.lang.DataOutputStream
![]() ![]() ![]() ![]() |
Input and Output Streams |