out.writeInt( 0 );
out.writeDouble( 12.45 );

A good answer might be:

An int is 4 bytes (32 bits), a double is 8 bytes (64 bits) so the total is 12 bytes.

The size of the number does not affect how many bytes are written. An int is 32 bits, regardless of its value.

The Byte Counter

A DataOutputStream counts the number of bytes that have been written to the stream. The method size() returns this value as an int. The following program shows this:

import java.io.*;
class ByteCounter
{
  public static void main ( String[] args ) throws IOException
  {
    String fileName = "mixedTypes.dat" ;

    DataOutputStream dataOut = new DataOutputStream(
        new BufferedOutputStream(
        new FileOutputStream( fileName  ) ) );

    dataOut.writeInt( 0 );
    System.out.println( dataOut.size()  + " bytes have been written.");

    dataOut.writeDouble( 12.45 );
    System.out.println( dataOut.size()  + " bytes have been written.");

    dataOut.close();
  }
}

The output from this program is:

C:\Programs>java  ByteCounter
4 bytes have been written.
12 bytes have been written.

C:\Programs>

The method returns the total count of bytes written, not the number just written.

QUESTION 12:

Can several different data types be written to the same file?