wood burning stoves 2.0*
The moose likes Java in General and the fly likes Reading a binary file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Reading a binary file" Watch "Reading a binary file" New topic
Author

Reading a binary file

Christopher Arthur
Ranch Hand

Joined: Mar 09, 2004
Posts: 149
Hi,

I have a file that I'm trying to read with Java. The file is binary set of records of fixed length, and each record is just a list of doubles. The only catch is that the file was created as output from a Fortran-77 program compiled and run on a UNIX machine, and I'm not sure how to deal with the possible difference in byte ordering. Isn't there just a readDouble() method that I can use, or will I have to do something fancier? I want the program to be platform-independent and I don't want to alter the datafiles.

Kindly,

Chris
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
java.io.DataInputStream has a readDouble() method, but it assumes the double value was created by java. I doubt it will work in a file created by Fortran. I suspect you'll have to figure out what the encoding is for Fortran, read the file a byte at a time and do your own decoding.


Merrill
Consultant, Sima Solutions
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

You have to find out what byte-ordering the UNIX machine used. If it was a Big-endian machine like a Sun SPARC or a 680x0, then you're all set, because Java uses Big-endian numbers. You could open the file with a DataInputStream and read the numbers directly.

If it was a Little-endian processor (x86) then that makes it a little harder. I think what I'd do would be to use the java.nio package, read the data into a ByteBuffer and construct a Little-endian DoubleBuffer from it.


[Jess in Action][AskingGoodQuestions]
Christopher Arthur
Ranch Hand

Joined: Mar 09, 2004
Posts: 149
Thank you so much for the help!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading a binary file