KevinSimonson wrote:
I'm planning on writing a program that stores a lot of binary informa-
tion, and I'd like to be able to write it to disk when I'm done with
it, so that later I can read it in from disk again. How do you do
that in Java, write binary <short>s to disk and then later read in
those <short>s from disk again?
IO-operations are byte-oriented (this is not java specific)
so when writing shorts (2 bytes) there are two ways to do
this and it depends if the data you write to disk should
be read by other programs as well. So before you use
DataOutputStream you should check if the format to be used
is LSB least significant byte first or HSB (high significant
byte first). HSB is used with DataOutputStream, if you
want to write LSB you need to do the writing for yourself:
outstream.write(myshort & 0xff);
outstream.write((myshort >> 8) & 0xff);
other direction:
short myshort = instream.read() & 0xff;
myshort |= (instream.read() << 8) & 0xff;