Skip to main content
gamedev.net
🔒 Locked

Thats why one can hate java

Started by DesignerX 6 replies 1.2k views
Original Post
DesignerX
DesignerX
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.
There is nothing that can't be solved. Just people that can't solve it. :)
hydroo
hydroo
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 )
smitty1276
smitty1276
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.
Excors
Excors
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.)
SunDog
SunDog
Easy.


public clas FontChar implments Serializable {
...
}


Now you can use in the IO methods as you can with strings, primtitives etc.
OrangyTang
OrangyTang
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.
benryves
benryves
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]
[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.