reading binary file.

E

El Durango

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

Gordon Beaton

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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top