home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / java / io / example / DataIOTest.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  2.7 KB  |  73 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import java.io.*;
  18.  
  19. class DataIOTest {
  20.     public static void main(String[] args) {
  21.  
  22.             // writing part
  23.         try {
  24.             DataOutputStream dos = new DataOutputStream(new FileOutputStream("invoice1.txt"));
  25.  
  26.             double[] prices = { 19.99, 9.99, 15.99, 3.99, 4.99 };
  27.             int[] units = { 12, 8, 13, 29, 50 };
  28.             String[] descs = { "Java T-shirt", "Java Mug", "Duke Juggling Dolls", "Java Pin", "Java Key Chain" };
  29.         
  30.             for (int i = 0; i < prices.length; i ++) {
  31.                 dos.writeDouble(prices[i]);
  32.                 dos.writeChar('\t');
  33.                 dos.writeInt(units[i]);
  34.                 dos.writeChar('\t');
  35.                 dos.writeChars(descs[i]);
  36.                 dos.writeChar('\n');
  37.             }
  38.             dos.close();
  39.         } catch (IOException e) {
  40.             System.out.println("DataIOTest: " + e);
  41.         }
  42.  
  43.             // reading part
  44.         try {
  45.             DataInputStream dis = new DataInputStream(new FileInputStream("invoice1.txt"));
  46.  
  47.             double price;
  48.             int unit;
  49.             String desc;
  50.             double total = 0.0;
  51.  
  52.             try {
  53.                 while (true) {
  54.                     price = dis.readDouble();
  55.                     dis.readChar();       // throws out the tab
  56.                     unit = dis.readInt();
  57.                     dis.readChar();       // throws out the tab
  58.                     desc = dis.readLine();
  59.                     System.out.println("You've ordered " + unit + " units of " + desc + " at $" + price);
  60.                     total = total + unit * price;
  61.                 }
  62.             } catch (EOFException e) {
  63.             }
  64.             System.out.println("For a TOTAL of: $" + total);
  65.             dis.close();
  66.         } catch (FileNotFoundException e) {
  67.             System.out.println("DataIOTest: " + e);
  68.         } catch (IOException e) {
  69.             System.out.println("DataIOTest: " + e);
  70.         }
  71.     }
  72. }
  73.