Jump to content
  • Advertisement
Sign in to follow this  
DesignerX

Thats why one can hate java

This topic is 4135 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I must say that writing binary data to file using java is the most frustrating thing I did. and maybe I did it wrong. For making things simple, lets say I have the following class : public class FontChar { public byte bChar; public short shXPos; public short shWidth; } and I want to write an instance of this class to a file (not as text). What is the simplest way to do this ? (I want also to be able to read it using c++ without revesing bytes like java does) thx.

Share this post


Link to post
Share on other sites
Advertisement
I guess the representation of the data is platform dependent. (little and big endian)
Its basically a bad idea to store classes to a file directly.

Maybe there exists some serialization approach for java. ( My java is pretty pour, sorry )

Share this post


Link to post
Share on other sites
Quote:
Maybe there exists some serialization approach for java.


There is a Serializable interface.

As hydroo said, I believe Java simply respects the endian-ness of the of the machine. You would have to check for that on both ends even (especially?) if you used C++ to write and read.

EDIT: I just looked things up, and I was wrong about it respecting the endian-ness of the machine... I see what you mean. It looks like it reads/writes in bigendian, even on a little endian machine.

This is apparently a common problem, and there are little endian output streams available online... you might want to check those out.

Share this post


Link to post
Share on other sites
DataOutputStream makes it fairly straightforward, except that's only big-endian. java.nio.ByteBuffer lets you set the byte order and then write ints/floats/etc, which seems much nicer. java.io.FileOutputStream's getChannel should give you a channel which implements ByteBuffer. (But I've never tried doing this myself, so I could be wrong.)

Share this post


Link to post
Share on other sites
Easy.


public clas FontChar implments Serializable {
...
}


Now you can use in the IO methods as you can with strings, primtitives etc.

Share this post


Link to post
Share on other sites
Quote:
Original post by SunDog
Easy.


public clas FontChar implments Serializable {
...
}


Now you can use in the IO methods as you can with strings, primtitives etc.

Serialisable isn't a good idea if you want to interchange with other languages, not only might the format change between JRE versions, but the format is far from a simple outputting of the fields of an object to disk. Excors' method would be how I'd do it.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
If time/space efficiency isn't an issue, you can also serialize every primitive type as a string. This way is totally platform independent.

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
If time/space efficiency isn't an issue, you can also serialize every primitive type as a string. This way is totally platform independent.
However, you now shift the problem onto the locale. "1.5" is good on an English system, but a French one would be expecting "1,5". [smile]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!