001package edu.pdx.cs410J.core;
002
003import java.io.*;
004
005/**
006 * This program reads <code>double</code>s from the command line and
007 * writes them to a file.  It demonstrates the
008 * <code>FileOutputStream</code> and <code>DataOutputStream</code>
009 * classes.
010 */
011public class WriteDoubles {
012
013  static PrintStream err = System.err;
014
015  /**
016   * The first command line argument is the name of the file, the
017   * remaining are the doubles
018   */
019  public static void main(String[] args) {
020    // Create a FileOutputStream
021    FileOutputStream fos = null;
022    try {
023      fos = new FileOutputStream(args[0]);
024
025    } catch (FileNotFoundException ex) {
026      err.println("** No such file: " + args[0]);
027      System.exit(1);
028    }
029
030    // Wrap a DataOutputStream around the FileOutputStream
031    DataOutputStream dos = new DataOutputStream(fos);
032
033    // Write the doubles to the DataOutputStream
034    for (int i = 1; i < args.length; i++) {
035      try {
036        double d = Double.parseDouble(args[i]);
037        dos.writeDouble(d);
038
039      } catch (NumberFormatException ex) {
040        err.println("** Not a double: " + args[i]);
041
042      } catch (IOException ex) {
043        err.println("** " + ex);
044        System.exit(1);
045      }
046    }
047  }
048}