Thats why one can hate java
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.
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 )
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 )
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.
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.)
Easy.
public clas FontChar implments Serializable {
...
}
Now you can use in the IO methods as you can with strings, primtitives etc.
public clas FontChar implments Serializable {
...
}
Now you can use in the IO methods as you can with strings, primtitives etc.
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.
If time/space efficiency isn't an issue, you can also serialize every primitive type as a string. This way is totally platform independent.
Quote:Original post by Anonymous PosterHowever, 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]
If time/space efficiency isn't an issue, you can also serialize every primitive type as a string. This way is totally platform independent.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement