go to previous page   go to home page   go to next page

Answer:

writeDouble()


writeDouble()

Here is a program that writes four doubles to a file.

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

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

    out.writeDouble( 0.0 );
    out.writeDouble( 1.0 );
    out.writeDouble( 255.0 );
    out.writeDouble( -1.0 );

    out.close();
  }
}

Primitive type double uses 64 bits, or eight bytes per value. The hex dump of doubleData.dat shows four groups of eight bytes, highlighted with red braces (which I added). The first group is recognizable as zero, but the others are mysterious. Floating point data are represented with a different style of bit patterns than integer data.

hex dump of a file

The right side of each line in the dump attempts to interpret the bytes as ASCII characters. The "." means that the byte does not contain a bit pattern that represents a character. Some bytes happen to contain a pattern that could be a character. One byte, part of the bytes that represent -1, contains a pattern that could represent the character "@". The byte after it contains a pattern that could represent "o".


QUESTION 9:

What data type does the constructor for FileOutputStream require?