import java.io.*;

    // Illustrate the DataInputStream and DataOutputStream
    // classes by writing some sample data, and then reading
    // it back in.
class DataStreamMain {
    public static void main(String[] args){
        try{
            // The name of the binary file to be written/read.
            final String datafile = "data.bin";
            writeData(datafile);
            readData(datafile);
        }
        catch(IOException e){
            System.err.println(e.toString());
        }
    }

        // Write a string, and an array of doubles to the given file.
    public static void writeData(String file) throws IOException {
        DataOutputStream out = null;
        try{
            final String message = "Einstein I'm not.";
            final double[] vals = { Math.PI, Math.E };
            final int numValues = vals.length;

            out = new DataOutputStream(new FileOutputStream(file));
            out.writeUTF(message);
            // Indicate how many values there are.
            out.writeInt(numValues);
            for(int i = 0; i < numValues; i++){
                out.writeDouble(vals[i]);
            }
        }
        finally{
            if(out != null){
                out.close();
            }
        }

    }
        // Read a string, and an array of doubles from the given file.
    public static void readData(String file) throws IOException {
        DataInputStream in = null;
        try{
            in = new DataInputStream(new FileInputStream(file));
            System.out.println(in.readUTF());
            // Read how many values there are.
            final int numValues = in.readInt();
            for(int i = 0; i < numValues; i++){
                System.out.print(in.readDouble()+" ");
            }
            System.out.println();
        }
        finally{
            if(in != null){
                in.close();
            }
        }
    }
}
