Thursday, March 12, 2009

Why is UTFDataFormatException thrown by DataOutputStream.writeUTF()

Question :Why is UTFDataFormatException thrown by DataOutputStream.writeUTF()
when serializing a String? (CoreJava)

Answer :DataOutputStream.writeUTF() does not support writing out strings larger
than 64K. The first two bytes of a UTF string in the stream are the length
of the string. If a java.lang.String can be larger than 64K, it needs to be
stored in the stream by an alternative method rather than depending on
the default method of storing a String in the stream, writeUTF.

No comments: