Sign in to follow this  
DesignerX

Thats why one can hate java

Recommended Posts

DesignerX    174
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
hydroo    295
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
smitty1276    560
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
Excors    715
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
SunDog    232
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
OrangyTang    1298
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   
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
benryves    1999
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this