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. Advertisements

  2. 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?
    /gordon
     
    Gordon Beaton, Dec 8, 2004
    #2
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.