Home » » Using DataInputStream and DataOutputStream --EOF

Using DataInputStream and DataOutputStream --EOF

Using DataInputStream and DataOutputStream--EOF

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.
19.99   12      Java T-shirt
9.99 8 Java Mug


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 named invoice1.txt.


DataOutputStream dos = new DataOutputStream(
new FileOutputStream("invoice1.txt"));


Next, DataIOTest uses DataOutputStream's specialized writeXXX() methods to write the invoice data (contained within arrays in the program).


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();


Note that this code snippet closes the output stream when its finished. The close() method flushes the stream before closing it.

Next, DataIOTest opens a DataInputStream on the file just written:




DataInputStream dis = new DataInputStream(
new FileInputStream("invoice1.txt"));


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 namedinvoice1.txt. DataIOTest then just reads the data back in using DataInputStream's specialized readXXX() methods to read the input data into Java variables of the correct type.


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();


When all of the data has been read, DataIOText displays a statement summarizing the order and the total amount owed, and closes the stream.

Note the loop that DataIOTest uses to read the data from the DataInputStream. Normally, when reading you see loops like this:




while ((input = dis.readLine()) != null) {
. . .
}


The 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 could readDouble() 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. The while 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.88

3 Comments:

John Hipo said...

Wow, this is truly impressive! Your dedication and hard work really shine through; I am stuck to do shopping for my upcoming friend wedding event so I need coupons heaven discount code to save my money for my university fee If you have any suggestion let me know

jennifer said...

When it comes to finding the best essay help uk, it's like having a supportive friend by your side. It offers clarity, guidance, and expertise, acting as a mentor to help you navigate academic challenges with confidence. It's more than just a service; it's a partnership built on trust and excellence, ensuring you exceed your academic goals.






mariava said...

When delving into the realm of Java programming, understanding tools like DataInputStream and DataOutputStream can significantly amplify your capabilities. As someone deeply involved in personal branding services, I often draw parallels between data handling in programming and managing personal brand narratives. Just as these tools facilitate precise data manipulation, effective personal branding involves crafting and conveying a clear, compelling message. Mastering DataInputStream and DataOutputStream empowers developers to control information flow, much like how strategic branding controls perception and impact. It's all about leveraging tools thoughtfully to enhance both technical prowess and personal influence.

Popular Posts