How to set the encodings of RandomAccessFile

B

Bruce

Hi, All,

I am writing a module needs to flush data into disk file, and this file
would be read on different platforms, including *nix, windows. I use
RandomAccessFile (java 1.4.2) to do the input and output operation. The
data I write are int, long, and bytes.

Now I simply write and read these data using writeLong, writeInt,
write(byte[]) and readLong, readInt, read(byte[]) funtions of
RandomAccessFile. I am not sure if there is encoding problem when the
file written on Windows be read on Linux/unix/solaris platform.

I look for functions of changing default RandomAccessFile's charset
encoding, but I didn't find it.

Regards,

-Bruce
 
R

Roedy Green

I am writing a module needs to flush data into disk file, and this file
would be read on different platforms, including *nix, windows. I use
RandomAccessFile (java 1.4.2) to do the input and output operation. The
data I write are int, long, and bytes.

RandomAccess File and encoding do not blend well together. When you
convert a String to bytes you get a VARIABLE length byte string.
Typically RandomAccessFiles are handle only fixed length fields.

What you can do is leave a max-sized hole, and plop the bytes in there
with a length byte,short, int count.

But you are doing your conversion on individual strings. See
http://mindprod.com/encoding.html for how to interconvert
byte[]/String.

It makes no sense to make the conversion part RandomAccessFile.
 
J

John C. Bollinger

Bruce said:
Hi, All,

I am writing a module needs to flush data into disk file, and this file
would be read on different platforms, including *nix, windows. I use
RandomAccessFile (java 1.4.2) to do the input and output operation. The
data I write are int, long, and bytes.

Now I simply write and read these data using writeLong, writeInt,
write(byte[]) and readLong, readInt, read(byte[]) funtions of
RandomAccessFile. I am not sure if there is encoding problem when the
file written on Windows be read on Linux/unix/solaris platform.

I look for functions of changing default RandomAccessFile's charset
encoding, but I didn't find it.

Encodings / charsets are specifically about converting characters to
bytes and back. They have nothing whatsoever to do with reading and
writing numeric datatypes. There *is* a potential portability issue
with regard to byte order for ints and longs, but RandomAccessFile
specifies the byte order that readInt, et. al. use. There is no byte
order issue with reading and writing byte arrays, but you do need to
watch out for partial reads -- not a big problem as long as you are
aware of it, and you may find that readFully() does what you really want
more easily.

The long and short of it, then, is that for the RandomAccessFile
behaviors you use are documented to be platform independent. Indeed,
the whole class appears to be free of platform dependencies involving
data representation.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top