I suggest you write the length first, then you can read back exactly that many bytes. You should always write you write
method to write out what you need to read in your "read" method, in the same order and format.
Unless you have good reason to do so, its simpler to use DataInput/DataOutputStream which support writeUTF/readUTF.