reading binary file.

Discussion in 'Java' started by El Durango, Dec 8, 2004.

  1. El Durango

    El Durango Guest

    I have to write Java to read a binary file and I am not getting the values I
    am looking for.
    The format of the file is:
    String 4bytes
    String 20bytes
    String 32bytes
    double 8bytes
    double 8bytes
    double 8bytes
    double 8bytes
    double 8bytes


    Here is a snippet of my code:
    fis = new FileInputStream(inputFile);
    dis = new DataInputStream(fis);
    while(retCode != -1){
    retCode = dis.read(caseNameBuf,offset,CASENAMESIZE);
    System.out.println(new String(caseNameBuf));
    if(retCode == -1){
    throw new EOFException();
    }
    retCode = dis.read(nullBuf,offset,12);
    System.out.println(new String(nullBuf));
    if(retCode == -1){
    throw new EOFException();
    }
    retCode = dis.read(eventNameBuf,offset,EVENTNAMESIZE);
    System.out.println(new String(eventNameBuf));
    if(retCode == -1){
    throw new EOFException();
    }
    d = dis.readDouble();
    System.out.println(d);
    d = dis.readDouble();
    System.out.println(d);
    d = dis.readDouble();
    System.out.println(d);
    d = dis.readDouble();
    System.out.println(d);
    d = dis.readDouble();
    System.out.println(d);
    }
    ...
    I am getting the correct String data however I cannot get the proper data
    for the double values. Can anyone inform me of what mistake I am making
    here.
    thank you,
    Durango.
     
    El Durango, Dec 8, 2004
    #1
    1. Advertising

  2. On Wed, 08 Dec 2004 05:56:59 GMT, El Durango wrote:
    > I have to write Java to read a binary file and I am not getting the
    > values I am looking for.
    >
    > The format of the file is:
    > String 4bytes
    > String 20bytes
    > String 32bytes
    > double 8bytes
    > double 8bytes
    > double 8bytes
    > double 8bytes
    > double 8bytes
    >


    [...]

    > I am getting the correct String data however I cannot get the proper
    > data for the double values. Can anyone inform me of what mistake I
    > am making here.


    How was the file created? How are the doubles represented in the file?

    Try reading the 8 byte doubles as bytes, then display each byte
    individually. Now read the descriptions of Double.DoubleToLongBits()
    and Double.longBitsToDouble():

    http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Double.html

    Can you see the relationship between the bytes you read and the format
    expected by DataInputStream.readDouble(), which you're using to read
    the file?

    > d = dis.readDouble();


    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Dec 8, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neo
    Replies:
    0
    Views:
    472
  2. Brad Marts

    Reading binary data from file

    Brad Marts, Dec 8, 2003, in forum: C++
    Replies:
    1
    Views:
    434
    Victor Bazarov
    Dec 8, 2003
  3. Ron Eggler

    writing binary file (ios::binary)

    Ron Eggler, Apr 25, 2008, in forum: C++
    Replies:
    9
    Views:
    936
    James Kanze
    Apr 28, 2008
  4. Guest
    Replies:
    6
    Views:
    1,692
    Guest
    Apr 25, 2010
  5. Jim
    Replies:
    6
    Views:
    736
Loading...

Share This Page