Example usage for java.io ObjectOutputStream writeDouble

List of usage examples for java.io ObjectOutputStream writeDouble

Introduction

In this page you can find the example usage for java.io ObjectOutputStream writeDouble.

Prototype

public void writeDouble(double val) throws IOException 

Source Link

Document

Writes a 64 bit double.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {

    double b = 123.234d;

    FileOutputStream out = new FileOutputStream("test.txt");
    ObjectOutputStream oout = new ObjectOutputStream(out);

    // write something in the file
    oout.writeDouble(b);
    oout.writeDouble(456.789d);// w  ww.  j av a 2 s  . c  o m
    oout.flush();
    oout.close();
    // create an ObjectInputStream for the file we created before
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

    // read and print a double
    System.out.println(ois.readDouble());

    // read and print a double
    System.out.println(ois.readDouble());
    ois.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {

    ObjectOutputStream out = new ObjectOutputStream(
            new BufferedOutputStream(new FileOutputStream("file.data")));
    out.writeDouble(1.234);
    out.close();/*from  w w w .  j a  v  a 2 s.c  o  m*/

    ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(new FileInputStream("file.data")));

    byte[] byteArray = new byte[10];
    in.read(byteArray);
    System.out.println(Arrays.toString(byteArray));
    in.close();
}

From source file:DataIOTest2.java

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]);//from  w ww  .jav a 2 s .c  o m
        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();
}

From source file:org.mrgeo.utils.Base64Utils.java

public static String encodeDoubleArray(double[] noData) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = null;
    byte[] rawBytes;
    try {//from  w w  w  . ja  v a 2s  . c  o  m
        oos = new ObjectOutputStream(baos);
        oos.writeInt(noData.length);
        for (int i = 0; i < noData.length; i++) {
            oos.writeDouble(noData[i]);
        }
        oos.flush();
        rawBytes = baos.toByteArray();
    } finally {
        if (oos != null) {
            oos.close();
        }
        baos.close();
    }

    return new String(Base64.encodeBase64(rawBytes));
}

From source file:Main.java

/**
 * Serialises a <code>Point2D</code> object.
 *
 * @param p  the point object (<code>null</code> permitted).
 * @param stream  the output stream (<code>null</code> not permitted).
 *
 * @throws IOException if there is an I/O error.
 *//* w ww  .j a va  2  s . c  o m*/
public static void writePoint2D(final Point2D p, final ObjectOutputStream stream) throws IOException {

    if (stream == null) {
        throw new IllegalArgumentException("Null 'stream' argument.");
    }
    if (p != null) {
        stream.writeBoolean(false);
        stream.writeDouble(p.getX());
        stream.writeDouble(p.getY());
    } else {
        stream.writeBoolean(true);
    }
}

From source file:Main.java

/**
 * Serialises a <code>Shape</code> object.
 *
 * @param shape  the shape object (<code>null</code> permitted).
 * @param stream  the output stream (<code>null</code> not permitted).
 *
 * @throws IOException if there is an I/O error.
 *//*w w w. j  a v  a  2 s .co m*/
public static void writeShape(final Shape shape, final ObjectOutputStream stream) throws IOException {

    if (stream == null) {
        throw new IllegalArgumentException("Null 'stream' argument.");
    }
    if (shape != null) {
        stream.writeBoolean(false);
        if (shape instanceof Line2D) {
            final Line2D line = (Line2D) shape;
            stream.writeObject(Line2D.class);
            stream.writeDouble(line.getX1());
            stream.writeDouble(line.getY1());
            stream.writeDouble(line.getX2());
            stream.writeDouble(line.getY2());
        } else if (shape instanceof Rectangle2D) {
            final Rectangle2D rectangle = (Rectangle2D) shape;
            stream.writeObject(Rectangle2D.class);
            stream.writeDouble(rectangle.getX());
            stream.writeDouble(rectangle.getY());
            stream.writeDouble(rectangle.getWidth());
            stream.writeDouble(rectangle.getHeight());
        } else if (shape instanceof Ellipse2D) {
            final Ellipse2D ellipse = (Ellipse2D) shape;
            stream.writeObject(Ellipse2D.class);
            stream.writeDouble(ellipse.getX());
            stream.writeDouble(ellipse.getY());
            stream.writeDouble(ellipse.getWidth());
            stream.writeDouble(ellipse.getHeight());
        } else if (shape instanceof Arc2D) {
            final Arc2D arc = (Arc2D) shape;
            stream.writeObject(Arc2D.class);
            stream.writeDouble(arc.getX());
            stream.writeDouble(arc.getY());
            stream.writeDouble(arc.getWidth());
            stream.writeDouble(arc.getHeight());
            stream.writeDouble(arc.getAngleStart());
            stream.writeDouble(arc.getAngleExtent());
            stream.writeInt(arc.getArcType());
        } else if (shape instanceof GeneralPath) {
            stream.writeObject(GeneralPath.class);
            final PathIterator pi = shape.getPathIterator(null);
            final float[] args = new float[6];
            stream.writeBoolean(pi.isDone());
            while (!pi.isDone()) {
                final int type = pi.currentSegment(args);
                stream.writeInt(type);
                // TODO: could write this to only stream the values
                // required for the segment type
                for (int i = 0; i < 6; i++) {
                    stream.writeFloat(args[i]);
                }
                stream.writeInt(pi.getWindingRule());
                pi.next();
                stream.writeBoolean(pi.isDone());
            }
        } else {
            stream.writeObject(shape.getClass());
            stream.writeObject(shape);
        }
    } else {
        stream.writeBoolean(true);
    }
}

From source file:edu.pdx.its.portal.routelandia.entities.Station.java

/**
 * implement write out LatLng object*//from www  .  java 2 s. c  o  m
 * @param out
 * @throws IOException
 */
private void writeObject(ObjectOutputStream out) throws IOException {
    for (int i = 0; i < latLngList.size(); i++) {
        out.writeDouble(latLngList.get(i).latitude);
        out.writeDouble(latLngList.get(i).longitude);
    }

}

From source file:DoubleList.java

private void writeObject(ObjectOutputStream out) throws IOException {
    out.writeInt(CURRENT_SERIAL_VERSION);
    int size = data.length;
    out.writeInt(size);//from   w ww .  ja va 2 s .  c om
    for (int i = 1; i < size; i++) {
        out.writeDouble(data[i]);
    }
    out.writeInt(this.size);
}

From source file:DoubleArrayList.java

private void writeObject(ObjectOutputStream out) throws IOException {
    out.defaultWriteObject();/*from  www. jav a2  s  .  c  om*/
    out.writeInt(array.length);
    for (int i = 0; i < size; i++) {
        out.writeDouble(array[i]);
    }
}

From source file:com.ojcoleman.ahni.util.DoubleVector.java

private void writeObject(ObjectOutputStream out) throws IOException {
    out.defaultWriteObject();//from w ww . ja v  a  2 s.  co m
    out.writeInt(_data.length);
    for (int i = 0; i < _size; i++) {
        out.writeDouble(_data[i]);
    }
}