home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / java / io / example-1.1 / DataIOTest.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  2.7 KB  |  75 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.err.println("DataIOTest: " + e);
  41.         }
  42.  
  43.             // reading part
  44.         try {
  45.             DataInputStream dis = new DataInputStream(new FileInputStream("invoice1.txt"));
  46.         PrintWriter stdout = new PrintWriter(System.out, true);
  47.  
  48.             double price;
  49.             int unit;
  50.             double total = 0.0;
  51.         char chr;
  52.  
  53.             try {
  54.                 while (true) {
  55.                     price = dis.readDouble();
  56.                     dis.readChar();       // throws out the tab
  57.                     unit = dis.readInt();
  58.             StringBuffer desc = new StringBuffer(20);
  59.             while ((chr = dis.readChar()) != '\n')
  60.                 desc.append(chr);
  61.                     stdout.println("You've ordered " + unit + " units of " + desc + " at $" + price);
  62.                     total = total + unit * price;
  63.                 }
  64.             } catch (EOFException e) {
  65.             }
  66.             stdout.println("For a TOTAL of: $" + total);
  67.             dis.close();
  68.         } catch (FileNotFoundException e) {
  69.             System.err.println("DataIOTest: " + e);
  70.         } catch (IOException e) {
  71.             System.err.println("DataIOTest: " + e);
  72.         }
  73.     }
  74. }
  75.