/* from http://java.sun.com/docs/books/tutorial/index.html */
import java.io.eofexception; import java.io.fileinputstream; import java.io.fileoutputstream; import java.io.ioexception; import java.io.objectinputstream; import java.io.objectoutputstream;
public class dataiotest2 { public static void main(string[] args) throws ioexception {
// write the data out objectoutputstream out = new objectoutputstream(new fileoutputstream( "invoice1.txt"));
double[] prices = { 19.99, 9.99, 15.99, 3.99, 4.99 }; int[] units = { 12, 8, 13, 29, 50 }; string[] descs = { "java t-shirt", "java mug", "duke juggling dolls", "java pin", "java key chain" };
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();
// read it in again objectinputstream in = new objectinputstream(new fileinputstream( "invoice1.txt"));
double price; int unit; string desc; double total = 0.0;
try { while (true) { price = in.readdouble(); in.readchar(); // throws out the tab unit = in.readint(); in.readchar(); // throws out the tab desc = in.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); in.close(); } }
|