• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reading a binary file

 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Christopher Arthur
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much for the help!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic