|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
This page shows you how to use thejava.ioDataInputStreamand
DataOutputStreamclasses. 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 aFileOutputStreamthat is set up to write to a file namedinvoice1.txt:Next,DataOutputStream out = new DataOutputStream( new FileOutputStream("invoice1.txt"));DataIODemousesDataOutputStream's specializedwritemethods 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();DataIODemoopens aDataInputStreamon the file just written:DataInputStream in = new DataInputStream( new FileInputStream("invoice1.txt"));DataInputStreamalso must be attached to anotherInputStream; in this case, aFileInputStreamset up to read the file just written,invoice1.txt. ThenDataIODemojust 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();DataIODemodisplays a statement summarizing the order and the total amount owed and then closes the stream.Note the loop that
DataIODemouses to read the data from theDataInputStream. Normally, when data is read, you see loops like this:Thewhile ((input = in.read()) != null) { . . . }readmethod returns a value,null, which indicates that the end of the file has been reached. Many of theDataInputStreamread 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-1to indicate end of file. Well, you can't, because-1is a legitimate value that can be read from the input stream, usingreadDouble,readInt, or one of the other methods that reads numbers. SoDataInputStreams read methods throw anEOFExceptioninstead. When theEOFExceptionoccurs, thewhile (true)terminates.When you run the
DataIODemoprogram 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-2004 Sun Microsystems, Inc. All rights reserved.