2

I have written information to a file in python using struct.pack eg.

out.write( struct.pack(">f", 1.1) );
out.write( struct.pack(">i", 12) );
out.write( struct.pack(">3s", "abc") );

Then I read it in java using DataInputStream and readInt, readFloat and readUTF. Reading the numbers works but as soon as I call readUTF() I get EOFException.

I assume this is because of the differences in the format of the string being written and the way java reads it, or am I doing something wrong?

If they are incompatible, is there another way to read and write strings?

1 Answer 1

4

The format expected by readUTF(), is documented here. In short, it expects a 16-bit, big-endian length followed by the bytes of the string. So, I think you could modify your pack call to look something like this:

s = "abc"
out.write( struct.pack(">H", len(s) ))
out.write( struct.pack(">%ds" % len(s), s ))

My Python is a little rusty, but I think that's close. It also assume that a short (the >H) is 16 bits.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.