import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class TEST { public static void main(String args[]) throws FileNotFoundException, IOException { TEST t = new TEST(); t.Read(); } public void Write() throws FileNotFoundException, IOException { DataOutputStream dos = new DataOutputStream(new FileOutputStream("Data.bin")); double price[] = {950, 450, 6}; int units[] = {3, 6, 9}; String itemDisc[] = {"IPOD", "CD Player", "HeadPhone"}; for (int x = 0; x < 3; x++) { //Write Price dos.writeDouble(price[x]); System.out.println(price[x] + " Has been written..."); dos.writeChar('\t'); System.out.println('\t' + " Has been written..."); //Write Units dos.writeInt(units[x]); System.out.println(units[x] + " Has been written..."); dos.writeChar('\t'); System.out.println('\t' + " Has been written..."); //Write Item Description dos.writeChars(itemDisc[x]); System.out.println(itemDisc[x] + " Has been written..."); dos.writeChar('\t'); System.out.println('\t' + " Has been written..."); } dos.close(); } public void Read() throws FileNotFoundException, IOException { DataInputStream dis = new DataInputStream(new FileInputStream("Data.bin")); double price; int unit; StringBuffer item; double totalInv = 0; char lineSep = '\t'; try { while (true) { price = dis.readDouble(); dis.readChar();//Ignore tab character... unit = dis.readInt(); dis.readChar();//Ignore tab character... char chr; item = new StringBuffer(20); //Read item while ((chr = dis.readChar()) != lineSep) { item.append(chr); } System.out.println("You've ordered " + unit + " units of " + item.toString() + " at $" + price); totalInv += (unit * price); } } catch (IOException ex) { } finally { if (totalInv > 0) { System.out.println("Total Invoice amount is :" + totalInv); } dis.close(); } } }